mirror of
https://github.com/fluxcd/flux2.git
synced 2025-10-29 23:37:47 +08:00
Add flux version to bootstrap commit messages
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
This commit is contained in:
@ -126,6 +126,14 @@ func bootstrapValidate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func generateInstallManifests(targetPath, namespace, tmpDir string, localManifests string) (string, error) {
|
func generateInstallManifests(targetPath, namespace, tmpDir string, localManifests string) (string, error) {
|
||||||
|
if bootstrapArgs.version == install.MakeDefaultOptions().Version {
|
||||||
|
version, err := install.GetLatestVersion()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
bootstrapArgs.version = version
|
||||||
|
}
|
||||||
|
|
||||||
opts := install.Options{
|
opts := install.Options{
|
||||||
BaseURL: localManifests,
|
BaseURL: localManifests,
|
||||||
Version: bootstrapArgs.version,
|
Version: bootstrapArgs.version,
|
||||||
|
|||||||
@ -125,13 +125,25 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
usedPath, bootstrapPathDiffers := checkIfBootstrapPathDiffers(ctx, kubeClient, rootArgs.namespace, filepath.ToSlash(githubArgs.path.String()))
|
usedPath, bootstrapPathDiffers := checkIfBootstrapPathDiffers(
|
||||||
|
ctx,
|
||||||
|
kubeClient,
|
||||||
|
rootArgs.namespace,
|
||||||
|
filepath.ToSlash(githubArgs.path.String()),
|
||||||
|
)
|
||||||
|
|
||||||
if bootstrapPathDiffers {
|
if bootstrapPathDiffers {
|
||||||
return fmt.Errorf("cluster already bootstrapped to %v path", usedPath)
|
return fmt.Errorf("cluster already bootstrapped to %v path", usedPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
repository, err := git.NewRepository(githubArgs.repository, githubArgs.owner, githubArgs.hostname, ghToken, "flux", githubArgs.owner+"@users.noreply.github.com")
|
repository, err := git.NewRepository(
|
||||||
|
githubArgs.repository,
|
||||||
|
githubArgs.owner,
|
||||||
|
githubArgs.hostname,
|
||||||
|
ghToken,
|
||||||
|
"flux",
|
||||||
|
githubArgs.owner+"@users.noreply.github.com",
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -190,13 +202,22 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
// generate install manifests
|
// generate install manifests
|
||||||
logger.Generatef("generating manifests")
|
logger.Generatef("generating manifests")
|
||||||
installManifest, err := generateInstallManifests(githubArgs.path.String(), rootArgs.namespace, tmpDir, bootstrapArgs.manifestsPath)
|
installManifest, err := generateInstallManifests(
|
||||||
|
githubArgs.path.String(),
|
||||||
|
rootArgs.namespace,
|
||||||
|
tmpDir,
|
||||||
|
bootstrapArgs.manifestsPath,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// stage install manifests
|
// stage install manifests
|
||||||
changed, err = repository.Commit(ctx, path.Join(githubArgs.path.String(), rootArgs.namespace), "Add manifests")
|
changed, err = repository.Commit(
|
||||||
|
ctx,
|
||||||
|
path.Join(githubArgs.path.String(), rootArgs.namespace),
|
||||||
|
fmt.Sprintf("Add flux %s components manifests", bootstrapArgs.version),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -270,13 +291,25 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
// configure repo synchronization
|
// configure repo synchronization
|
||||||
logger.Actionf("generating sync manifests")
|
logger.Actionf("generating sync manifests")
|
||||||
syncManifests, err := generateSyncManifests(repoURL, bootstrapArgs.branch, rootArgs.namespace, rootArgs.namespace, filepath.ToSlash(githubArgs.path.String()), tmpDir, githubArgs.interval)
|
syncManifests, err := generateSyncManifests(
|
||||||
|
repoURL,
|
||||||
|
bootstrapArgs.branch,
|
||||||
|
rootArgs.namespace,
|
||||||
|
rootArgs.namespace,
|
||||||
|
filepath.ToSlash(githubArgs.path.String()),
|
||||||
|
tmpDir,
|
||||||
|
githubArgs.interval,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// commit and push manifests
|
// commit and push manifests
|
||||||
if changed, err = repository.Commit(ctx, path.Join(githubArgs.path.String(), rootArgs.namespace), "Add manifests"); err != nil {
|
if changed, err = repository.Commit(
|
||||||
|
ctx,
|
||||||
|
path.Join(githubArgs.path.String(), rootArgs.namespace),
|
||||||
|
fmt.Sprintf("Add flux %s sync manifests", bootstrapArgs.version),
|
||||||
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if changed {
|
} else if changed {
|
||||||
if err := repository.Push(ctx); err != nil {
|
if err := repository.Push(ctx); err != nil {
|
||||||
|
|||||||
@ -131,7 +131,14 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
return fmt.Errorf("cluster already bootstrapped to %v path", usedPath)
|
return fmt.Errorf("cluster already bootstrapped to %v path", usedPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
repository, err := git.NewRepository(gitlabArgs.repository, gitlabArgs.owner, gitlabArgs.hostname, glToken, "flux", gitlabArgs.owner+"@users.noreply.gitlab.com")
|
repository, err := git.NewRepository(
|
||||||
|
gitlabArgs.repository,
|
||||||
|
gitlabArgs.owner,
|
||||||
|
gitlabArgs.hostname,
|
||||||
|
glToken,
|
||||||
|
"flux",
|
||||||
|
gitlabArgs.owner+"@users.noreply.gitlab.com",
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -169,13 +176,22 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
// generate install manifests
|
// generate install manifests
|
||||||
logger.Generatef("generating manifests")
|
logger.Generatef("generating manifests")
|
||||||
installManifest, err := generateInstallManifests(gitlabArgs.path.String(), rootArgs.namespace, tmpDir, bootstrapArgs.manifestsPath)
|
installManifest, err := generateInstallManifests(
|
||||||
|
gitlabArgs.path.String(),
|
||||||
|
rootArgs.namespace,
|
||||||
|
tmpDir,
|
||||||
|
bootstrapArgs.manifestsPath,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// stage install manifests
|
// stage install manifests
|
||||||
changed, err = repository.Commit(ctx, path.Join(gitlabArgs.path.String(), rootArgs.namespace), "Add manifests")
|
changed, err = repository.Commit(
|
||||||
|
ctx,
|
||||||
|
path.Join(gitlabArgs.path.String(), rootArgs.namespace),
|
||||||
|
fmt.Sprintf("Add flux %s components manifests", bootstrapArgs.version),
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -249,13 +265,25 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
// configure repo synchronization
|
// configure repo synchronization
|
||||||
logger.Actionf("generating sync manifests")
|
logger.Actionf("generating sync manifests")
|
||||||
syncManifests, err := generateSyncManifests(repoURL, bootstrapArgs.branch, rootArgs.namespace, rootArgs.namespace, filepath.ToSlash(gitlabArgs.path.String()), tmpDir, gitlabArgs.interval)
|
syncManifests, err := generateSyncManifests(
|
||||||
|
repoURL,
|
||||||
|
bootstrapArgs.branch,
|
||||||
|
rootArgs.namespace,
|
||||||
|
rootArgs.namespace,
|
||||||
|
filepath.ToSlash(gitlabArgs.path.String()),
|
||||||
|
tmpDir,
|
||||||
|
gitlabArgs.interval,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// commit and push manifests
|
// commit and push manifests
|
||||||
if changed, err = repository.Commit(ctx, path.Join(gitlabArgs.path.String(), rootArgs.namespace), "Add manifests"); err != nil {
|
if changed, err = repository.Commit(
|
||||||
|
ctx,
|
||||||
|
path.Join(gitlabArgs.path.String(), rootArgs.namespace),
|
||||||
|
fmt.Sprintf("Add flux %s sync manifests", bootstrapArgs.version),
|
||||||
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if changed {
|
} else if changed {
|
||||||
if err := repository.Push(ctx); err != nil {
|
if err := repository.Push(ctx); err != nil {
|
||||||
|
|||||||
@ -122,6 +122,13 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if installVersion == install.MakeDefaultOptions().Version {
|
||||||
|
installVersion, err = install.GetLatestVersion()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
opts := install.Options{
|
opts := install.Options{
|
||||||
BaseURL: installManifestsPath,
|
BaseURL: installManifestsPath,
|
||||||
Version: installVersion,
|
Version: installVersion,
|
||||||
@ -156,7 +163,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
fmt.Print(manifest.Content)
|
fmt.Print(manifest.Content)
|
||||||
} else if installExport {
|
} else if installExport {
|
||||||
fmt.Println("---")
|
fmt.Println("---")
|
||||||
fmt.Println("# GitOps Toolkit revision", installVersion)
|
fmt.Println("# Flux version:", installVersion)
|
||||||
fmt.Println("# Components:", strings.Join(components, ","))
|
fmt.Println("# Components:", strings.Join(components, ","))
|
||||||
fmt.Print(manifest.Content)
|
fmt.Print(manifest.Content)
|
||||||
fmt.Println("---")
|
fmt.Println("---")
|
||||||
|
|||||||
@ -18,11 +18,14 @@ package install
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
securejoin "github.com/cyphar/filepath-securejoin"
|
securejoin "github.com/cyphar/filepath-securejoin"
|
||||||
|
|
||||||
@ -75,3 +78,29 @@ func Generate(options Options) (*manifestgen.Manifest, error) {
|
|||||||
Content: string(content),
|
Content: string(content),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetLatestVersion calls the GitHub API and returns the latest released version.
|
||||||
|
func GetLatestVersion() (string, error) {
|
||||||
|
ghURL := "https://api.github.com/repos/fluxcd/flux2/releases/latest"
|
||||||
|
c := http.DefaultClient
|
||||||
|
c.Timeout = 15 * time.Second
|
||||||
|
|
||||||
|
res, err := c.Get(ghURL)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("calling GitHub API failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if res.Body != nil {
|
||||||
|
defer res.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
type meta struct {
|
||||||
|
Tag string `json:"tag_name"`
|
||||||
|
}
|
||||||
|
var m meta
|
||||||
|
if err := json.NewDecoder(res.Body).Decode(&m); err != nil {
|
||||||
|
return "", fmt.Errorf("decoding GitHub API response failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.Tag, err
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user