Merge pull request #67 from fluxcd/log-interface

Create logger interface
pull/70/head
Hidde Beydals 5 years ago committed by GitHub
commit f4b2a32a23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -179,7 +179,7 @@ func applySyncManifests(ctx context.Context, kubeClient client.Client, name, nam
return err return err
} }
logWaiting("waiting for cluster sync") logger.Waitingf("waiting for cluster sync")
if err := wait.PollImmediate(pollInterval, timeout, if err := wait.PollImmediate(pollInterval, timeout,
isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil { isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil {

@ -118,13 +118,13 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
defer cancel() defer cancel()
// create GitHub repository if doesn't exists // create GitHub repository if doesn't exists
logAction("connecting to %s", ghHostname) logger.Actionf("connecting to %s", ghHostname)
changed, err := provider.CreateRepository(ctx, repository) changed, err := provider.CreateRepository(ctx, repository)
if err != nil { if err != nil {
return err return err
} }
if changed { if changed {
logSuccess("repository created") logger.Successf("repository created")
} }
withErrors := false withErrors := false
@ -132,10 +132,10 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
if !ghPersonal { if !ghPersonal {
for _, team := range ghTeams { for _, team := range ghTeams {
if changed, err := provider.AddTeam(ctx, repository, team, ghDefaultPermission); err != nil { if changed, err := provider.AddTeam(ctx, repository, team, ghDefaultPermission); err != nil {
logFailure(err.Error()) logger.Failuref(err.Error())
withErrors = true withErrors = true
} else if changed { } else if changed {
logSuccess("%s team access granted", team) logger.Successf("%s team access granted", team)
} }
} }
} }
@ -144,10 +144,10 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
if err := repository.Checkout(ctx, bootstrapBranch, tmpDir); err != nil { if err := repository.Checkout(ctx, bootstrapBranch, tmpDir); err != nil {
return err return err
} }
logSuccess("repository cloned") logger.Successf("repository cloned")
// generate install manifests // generate install manifests
logGenerate("generating manifests") logger.Generatef("generating manifests")
manifest, err := generateInstallManifests(ghPath, namespace, tmpDir) manifest, err := generateInstallManifests(ghPath, namespace, tmpDir)
if err != nil { if err != nil {
return err return err
@ -164,9 +164,9 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
if err := repository.Push(ctx); err != nil { if err := repository.Push(ctx); err != nil {
return err return err
} }
logSuccess("components manifests pushed") logger.Successf("components manifests pushed")
} else { } else {
logSuccess("components are up to date") logger.Successf("components are up to date")
} }
// determine if repo synchronization is working // determine if repo synchronization is working
@ -174,16 +174,16 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
if isInstall { if isInstall {
// apply install manifests // apply install manifests
logAction("installing components in %s namespace", namespace) logger.Actionf("installing components in %s namespace", namespace)
if err := applyInstallManifests(ctx, manifest, components); err != nil { if err := applyInstallManifests(ctx, manifest, components); err != nil {
return err return err
} }
logSuccess("install completed") logger.Successf("install completed")
} }
// setup SSH deploy key // setup SSH deploy key
if shouldCreateDeployKey(ctx, kubeClient, namespace) { if shouldCreateDeployKey(ctx, kubeClient, namespace) {
logAction("configuring deploy key") logger.Actionf("configuring deploy key")
u, err := url.Parse(repository.GetSSH()) u, err := url.Parse(repository.GetSSH())
if err != nil { if err != nil {
return fmt.Errorf("git URL parse failed: %w", err) return fmt.Errorf("git URL parse failed: %w", err)
@ -202,14 +202,14 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
if changed, err := provider.AddDeployKey(ctx, repository, key, keyName); err != nil { if changed, err := provider.AddDeployKey(ctx, repository, key, keyName); err != nil {
return err return err
} else if changed { } else if changed {
logSuccess("deploy key configured") logger.Successf("deploy key configured")
} }
} }
// configure repo synchronization // configure repo synchronization
if isInstall { if isInstall {
// generate source and kustomization manifests // generate source and kustomization manifests
logAction("generating sync manifests") logger.Actionf("generating sync manifests")
if err := generateSyncManifests(repository.GetSSH(), namespace, namespace, ghPath, tmpDir, ghInterval); err != nil { if err := generateSyncManifests(repository.GetSSH(), namespace, namespace, ghPath, tmpDir, ghInterval); err != nil {
return err return err
} }
@ -221,11 +221,11 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
if err := repository.Push(ctx); err != nil { if err := repository.Push(ctx); err != nil {
return err return err
} }
logSuccess("sync manifests pushed") logger.Successf("sync manifests pushed")
} }
// apply manifests and waiting for sync // apply manifests and waiting for sync
logAction("applying sync manifests") logger.Actionf("applying sync manifests")
if err := applySyncManifests(ctx, kubeClient, namespace, namespace, ghPath, tmpDir); err != nil { if err := applySyncManifests(ctx, kubeClient, namespace, namespace, ghPath, tmpDir); err != nil {
return err return err
} }
@ -235,6 +235,6 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("bootstrap completed with errors") return fmt.Errorf("bootstrap completed with errors")
} }
logSuccess("bootstrap finished") logger.Successf("bootstrap finished")
return nil return nil
} }

@ -109,23 +109,23 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
defer cancel() defer cancel()
// create GitLab project if doesn't exists // create GitLab project if doesn't exists
logAction("connecting to %s", glHostname) logger.Actionf("connecting to %s", glHostname)
changed, err := provider.CreateRepository(ctx, repository) changed, err := provider.CreateRepository(ctx, repository)
if err != nil { if err != nil {
return err return err
} }
if changed { if changed {
logSuccess("repository created") logger.Successf("repository created")
} }
// clone repository and checkout the master branch // clone repository and checkout the master branch
if err := repository.Checkout(ctx, bootstrapBranch, tmpDir); err != nil { if err := repository.Checkout(ctx, bootstrapBranch, tmpDir); err != nil {
return err return err
} }
logSuccess("repository cloned") logger.Successf("repository cloned")
// generate install manifests // generate install manifests
logGenerate("generating manifests") logger.Generatef("generating manifests")
manifest, err := generateInstallManifests(glPath, namespace, tmpDir) manifest, err := generateInstallManifests(glPath, namespace, tmpDir)
if err != nil { if err != nil {
return err return err
@ -142,9 +142,9 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
if err := repository.Push(ctx); err != nil { if err := repository.Push(ctx); err != nil {
return err return err
} }
logSuccess("components manifests pushed") logger.Successf("components manifests pushed")
} else { } else {
logSuccess("components are up to date") logger.Successf("components are up to date")
} }
// determine if repo synchronization is working // determine if repo synchronization is working
@ -152,16 +152,16 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
if isInstall { if isInstall {
// apply install manifests // apply install manifests
logAction("installing components in %s namespace", namespace) logger.Actionf("installing components in %s namespace", namespace)
if err := applyInstallManifests(ctx, manifest, components); err != nil { if err := applyInstallManifests(ctx, manifest, components); err != nil {
return err return err
} }
logSuccess("install completed") logger.Successf("install completed")
} }
// setup SSH deploy key // setup SSH deploy key
if shouldCreateDeployKey(ctx, kubeClient, namespace) { if shouldCreateDeployKey(ctx, kubeClient, namespace) {
logAction("configuring deploy key") logger.Actionf("configuring deploy key")
u, err := url.Parse(repository.GetSSH()) u, err := url.Parse(repository.GetSSH())
if err != nil { if err != nil {
return fmt.Errorf("git URL parse failed: %w", err) return fmt.Errorf("git URL parse failed: %w", err)
@ -180,14 +180,14 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
if changed, err := provider.AddDeployKey(ctx, repository, key, keyName); err != nil { if changed, err := provider.AddDeployKey(ctx, repository, key, keyName); err != nil {
return err return err
} else if changed { } else if changed {
logSuccess("deploy key configured") logger.Successf("deploy key configured")
} }
} }
// configure repo synchronization // configure repo synchronization
if isInstall { if isInstall {
// generate source and kustomization manifests // generate source and kustomization manifests
logAction("generating sync manifests") logger.Actionf("generating sync manifests")
if err := generateSyncManifests(repository.GetSSH(), namespace, namespace, glPath, tmpDir, glInterval); err != nil { if err := generateSyncManifests(repository.GetSSH(), namespace, namespace, glPath, tmpDir, glInterval); err != nil {
return err return err
} }
@ -199,16 +199,16 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
if err := repository.Push(ctx); err != nil { if err := repository.Push(ctx); err != nil {
return err return err
} }
logSuccess("sync manifests pushed") logger.Successf("sync manifests pushed")
} }
// apply manifests and waiting for sync // apply manifests and waiting for sync
logAction("applying sync manifests") logger.Actionf("applying sync manifests")
if err := applySyncManifests(ctx, kubeClient, namespace, namespace, glPath, tmpDir); err != nil { if err := applySyncManifests(ctx, kubeClient, namespace, namespace, glPath, tmpDir); err != nil {
return err return err
} }
} }
logSuccess("bootstrap finished") logger.Successf("bootstrap finished")
return nil return nil
} }

@ -58,7 +58,7 @@ func runCheckCmd(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel() defer cancel()
logAction("checking prerequisites") logger.Actionf("checking prerequisites")
checkFailed := false checkFailed := false
if !kubectlCheck(ctx, ">=1.18.0") { if !kubectlCheck(ctx, ">=1.18.0") {
@ -73,83 +73,83 @@ func runCheckCmd(cmd *cobra.Command, args []string) error {
if checkFailed { if checkFailed {
os.Exit(1) os.Exit(1)
} }
logSuccess("prerequisites checks passed") logger.Successf("prerequisites checks passed")
return nil return nil
} }
logAction("checking controllers") logger.Actionf("checking controllers")
if !componentsCheck() { if !componentsCheck() {
checkFailed = true checkFailed = true
} }
if checkFailed { if checkFailed {
os.Exit(1) os.Exit(1)
} }
logSuccess("all checks passed") logger.Successf("all checks passed")
return nil return nil
} }
func kubectlCheck(ctx context.Context, version string) bool { func kubectlCheck(ctx context.Context, version string) bool {
_, err := exec.LookPath("kubectl") _, err := exec.LookPath("kubectl")
if err != nil { if err != nil {
logFailure("kubectl not found") logger.Failuref("kubectl not found")
return false return false
} }
command := "kubectl version --client --short | awk '{ print $3 }'" command := "kubectl version --client --short | awk '{ print $3 }'"
output, err := utils.execCommand(ctx, ModeCapture, command) output, err := utils.execCommand(ctx, ModeCapture, command)
if err != nil { if err != nil {
logFailure("kubectl version can't be determined") logger.Failuref("kubectl version can't be determined")
return false return false
} }
v, err := semver.ParseTolerant(output) v, err := semver.ParseTolerant(output)
if err != nil { if err != nil {
logFailure("kubectl version can't be parsed") logger.Failuref("kubectl version can't be parsed")
return false return false
} }
rng, _ := semver.ParseRange(version) rng, _ := semver.ParseRange(version)
if !rng(v) { if !rng(v) {
logFailure("kubectl version must be %s", version) logger.Failuref("kubectl version must be %s", version)
return false return false
} }
logSuccess("kubectl %s %s", v.String(), version) logger.Successf("kubectl %s %s", v.String(), version)
return true return true
} }
func kubernetesCheck(version string) bool { func kubernetesCheck(version string) bool {
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig) cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil { if err != nil {
logFailure("Kubernetes client initialization failed: %s", err.Error()) logger.Failuref("Kubernetes client initialization failed: %s", err.Error())
return false return false
} }
client, err := kubernetes.NewForConfig(cfg) client, err := kubernetes.NewForConfig(cfg)
if err != nil { if err != nil {
logFailure("Kubernetes client initialization failed: %s", err.Error()) logger.Failuref("Kubernetes client initialization failed: %s", err.Error())
return false return false
} }
ver, err := client.Discovery().ServerVersion() ver, err := client.Discovery().ServerVersion()
if err != nil { if err != nil {
logFailure("Kubernetes API call failed: %s", err.Error()) logger.Failuref("Kubernetes API call failed: %s", err.Error())
return false return false
} }
v, err := semver.ParseTolerant(ver.String()) v, err := semver.ParseTolerant(ver.String())
if err != nil { if err != nil {
logFailure("Kubernetes version can't be determined") logger.Failuref("Kubernetes version can't be determined")
return false return false
} }
rng, _ := semver.ParseRange(version) rng, _ := semver.ParseRange(version)
if !rng(v) { if !rng(v) {
logFailure("Kubernetes version must be %s", version) logger.Failuref("Kubernetes version must be %s", version)
return false return false
} }
logSuccess("Kubernetes %s %s", v.String(), version) logger.Successf("Kubernetes %s %s", v.String(), version)
return true return true
} }
@ -162,10 +162,10 @@ func componentsCheck() bool {
command := fmt.Sprintf("kubectl -n %s rollout status deployment %s --timeout=%s", command := fmt.Sprintf("kubectl -n %s rollout status deployment %s --timeout=%s",
namespace, deployment, timeout.String()) namespace, deployment, timeout.String())
if output, err := utils.execCommand(ctx, ModeCapture, command); err != nil { if output, err := utils.execCommand(ctx, ModeCapture, command); err != nil {
logFailure("%s: %s", deployment, strings.TrimSuffix(output, "\n")) logger.Failuref("%s: %s", deployment, strings.TrimSuffix(output, "\n"))
ok = false ok = false
} else { } else {
logSuccess("%s is healthy", deployment) logger.Successf("%s is healthy", deployment)
} }
} }
return ok return ok

@ -122,7 +122,7 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error {
} }
if !export { if !export {
logGenerate("generating kustomization") logger.Generatef("generating kustomization")
} }
emptyAPIGroup := "" emptyAPIGroup := ""
@ -192,18 +192,18 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error {
return exportKs(kustomization) return exportKs(kustomization)
} }
logAction("applying kustomization") logger.Actionf("applying kustomization")
if err := upsertKustomization(ctx, kubeClient, kustomization); err != nil { if err := upsertKustomization(ctx, kubeClient, kustomization); err != nil {
return err return err
} }
logWaiting("waiting for kustomization sync") logger.Waitingf("waiting for kustomization sync")
if err := wait.PollImmediate(pollInterval, timeout, if err := wait.PollImmediate(pollInterval, timeout,
isKustomizationReady(ctx, kubeClient, name, namespace)); err != nil { isKustomizationReady(ctx, kubeClient, name, namespace)); err != nil {
return err return err
} }
logSuccess("kustomization %s is ready", name) logger.Successf("kustomization %s is ready", name)
namespacedName := types.NamespacedName{ namespacedName := types.NamespacedName{
Namespace: namespace, Namespace: namespace,
@ -215,7 +215,7 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error {
} }
if kustomization.Status.LastAppliedRevision != "" { if kustomization.Status.LastAppliedRevision != "" {
logSuccess("applied revision %s", kustomization.Status.LastAppliedRevision) logger.Successf("applied revision %s", kustomization.Status.LastAppliedRevision)
} else { } else {
return fmt.Errorf("kustomization sync failed") return fmt.Errorf("kustomization sync failed")
} }
@ -236,7 +236,7 @@ func upsertKustomization(ctx context.Context, kubeClient client.Client, kustomiz
if err := kubeClient.Create(ctx, &kustomization); err != nil { if err := kubeClient.Create(ctx, &kustomization); err != nil {
return err return err
} else { } else {
logSuccess("kustomization created") logger.Successf("kustomization created")
return nil return nil
} }
} }
@ -248,7 +248,7 @@ func upsertKustomization(ctx context.Context, kubeClient client.Client, kustomiz
return err return err
} }
logSuccess("kustomization updated") logger.Successf("kustomization updated")
return nil return nil
} }

@ -166,7 +166,7 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
withAuth := false withAuth := false
// TODO(hidde): move all auth prep to separate func? // TODO(hidde): move all auth prep to separate func?
if u.Scheme == "ssh" { if u.Scheme == "ssh" {
logAction("generating deploy key pair") logger.Actionf("generating deploy key pair")
pair, err := generateKeyPair(ctx) pair, err := generateKeyPair(ctx)
if err != nil { if err != nil {
return err return err
@ -181,15 +181,15 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("aborting") return fmt.Errorf("aborting")
} }
logAction("collecting preferred public key from SSH server") logger.Actionf("collecting preferred public key from SSH server")
hostKey, err := scanHostKey(ctx, u) hostKey, err := scanHostKey(ctx, u)
if err != nil { if err != nil {
return err return err
} }
logSuccess("collected public key from SSH server:") logger.Successf("collected public key from SSH server:")
fmt.Printf("%s", hostKey) fmt.Printf("%s", hostKey)
logAction("applying secret with keys") logger.Actionf("applying secret with keys")
secret := corev1.Secret{ secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: name, Name: name,
@ -206,7 +206,7 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
} }
withAuth = true withAuth = true
} else if sourceGitUsername != "" && sourceGitPassword != "" { } else if sourceGitUsername != "" && sourceGitPassword != "" {
logAction("applying secret with basic auth credentials") logger.Actionf("applying secret with basic auth credentials")
secret := corev1.Secret{ secret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: name, Name: name,
@ -224,10 +224,10 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
} }
if withAuth { if withAuth {
logSuccess("authentication configured") logger.Successf("authentication configured")
} }
logGenerate("generating source") logger.Generatef("generating source")
if withAuth { if withAuth {
gitRepository.Spec.SecretRef = &corev1.LocalObjectReference{ gitRepository.Spec.SecretRef = &corev1.LocalObjectReference{
@ -235,18 +235,18 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
} }
} }
logAction("applying source") logger.Actionf("applying source")
if err := upsertGitRepository(ctx, kubeClient, gitRepository); err != nil { if err := upsertGitRepository(ctx, kubeClient, gitRepository); err != nil {
return err return err
} }
logWaiting("waiting for git sync") logger.Waitingf("waiting for git sync")
if err := wait.PollImmediate(pollInterval, timeout, if err := wait.PollImmediate(pollInterval, timeout,
isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil { isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil {
return err return err
} }
logSuccess("git sync completed") logger.Successf("git sync completed")
namespacedName := types.NamespacedName{ namespacedName := types.NamespacedName{
Namespace: namespace, Namespace: namespace,
@ -258,7 +258,7 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error {
} }
if gitRepository.Status.Artifact != nil { if gitRepository.Status.Artifact != nil {
logSuccess("fetched revision: %s", gitRepository.Status.Artifact.Revision) logger.Successf("fetched revision: %s", gitRepository.Status.Artifact.Revision)
} else { } else {
return fmt.Errorf("git sync failed, artifact not found") return fmt.Errorf("git sync failed, artifact not found")
} }
@ -336,7 +336,7 @@ func upsertGitRepository(ctx context.Context, kubeClient client.Client, gitRepos
if err := kubeClient.Create(ctx, &gitRepository); err != nil { if err := kubeClient.Create(ctx, &gitRepository); err != nil {
return err return err
} else { } else {
logSuccess("source created") logger.Successf("source created")
return nil return nil
} }
} }
@ -348,7 +348,7 @@ func upsertGitRepository(ctx context.Context, kubeClient client.Client, gitRepos
return err return err
} }
logSuccess("source updated") logger.Successf("source updated")
return nil return nil
} }

@ -65,7 +65,7 @@ func deleteKsCmdRun(cmd *cobra.Command, args []string) error {
if !deleteSilent { if !deleteSilent {
if !kustomization.Spec.Suspend { if !kustomization.Spec.Suspend {
logWaiting("This action will remove the Kubernetes objects previously applied by the %s kustomization!", name) logger.Waitingf("This action will remove the Kubernetes objects previously applied by the %s kustomization!", name)
} }
prompt := promptui.Prompt{ prompt := promptui.Prompt{
Label: "Are you sure you want to delete this kustomization", Label: "Are you sure you want to delete this kustomization",
@ -76,12 +76,12 @@ func deleteKsCmdRun(cmd *cobra.Command, args []string) error {
} }
} }
logAction("deleting kustomization %s in %s namespace", name, namespace) logger.Actionf("deleting kustomization %s in %s namespace", name, namespace)
err = kubeClient.Delete(ctx, &kustomization) err = kubeClient.Delete(ctx, &kustomization)
if err != nil { if err != nil {
return err return err
} }
logSuccess("kustomization deleted") logger.Successf("kustomization deleted")
return nil return nil
} }

@ -72,12 +72,12 @@ func deleteSourceGitCmdRun(cmd *cobra.Command, args []string) error {
} }
} }
logAction("deleting source %s in %s namespace", name, namespace) logger.Actionf("deleting source %s in %s namespace", name, namespace)
err = kubeClient.Delete(ctx, &git) err = kubeClient.Delete(ctx, &git)
if err != nil { if err != nil {
return err return err
} }
logSuccess("source deleted") logger.Successf("source deleted")
return nil return nil
} }

@ -67,7 +67,7 @@ func exportKsCmdRun(cmd *cobra.Command, args []string) error {
} }
if len(list.Items) == 0 { if len(list.Items) == 0 {
logFailure("no kustomizations found in %s namespace", namespace) logger.Failuref("no kustomizations found in %s namespace", namespace)
return nil return nil
} }

@ -67,7 +67,7 @@ func exportSourceGitCmdRun(cmd *cobra.Command, args []string) error {
} }
if len(list.Items) == 0 { if len(list.Items) == 0 {
logFailure("no source found in %s namespace", namespace) logger.Failuref("no source found in %s namespace", namespace)
return nil return nil
} }

@ -53,13 +53,13 @@ func getKsCmdRun(cmd *cobra.Command, args []string) error {
} }
if len(list.Items) == 0 { if len(list.Items) == 0 {
logFailure("no kustomizations found in %s namespace", namespace) logger.Failuref("no kustomizations found in %s namespace", namespace)
return nil return nil
} }
for _, kustomization := range list.Items { for _, kustomization := range list.Items {
if kustomization.Spec.Suspend { if kustomization.Spec.Suspend {
logSuccess("%s is suspended", kustomization.GetName()) logger.Successf("%s is suspended", kustomization.GetName())
continue continue
} }
isInitialized := false isInitialized := false
@ -67,19 +67,19 @@ func getKsCmdRun(cmd *cobra.Command, args []string) error {
if condition.Type == kustomizev1.ReadyCondition { if condition.Type == kustomizev1.ReadyCondition {
if condition.Status != corev1.ConditionFalse { if condition.Status != corev1.ConditionFalse {
if kustomization.Status.LastAppliedRevision != "" { if kustomization.Status.LastAppliedRevision != "" {
logSuccess("%s last applied revision %s", kustomization.GetName(), kustomization.Status.LastAppliedRevision) logger.Successf("%s last applied revision %s", kustomization.GetName(), kustomization.Status.LastAppliedRevision)
} else { } else {
logSuccess("%s reconciling", kustomization.GetName()) logger.Successf("%s reconciling", kustomization.GetName())
} }
} else { } else {
logFailure("%s %s", kustomization.GetName(), condition.Message) logger.Failuref("%s %s", kustomization.GetName(), condition.Message)
} }
isInitialized = true isInitialized = true
break break
} }
} }
if !isInitialized { if !isInitialized {
logFailure("%s is not ready", kustomization.GetName()) logger.Failuref("%s is not ready", kustomization.GetName())
} }
} }
return nil return nil

@ -52,7 +52,7 @@ func getSourceGitCmdRun(cmd *cobra.Command, args []string) error {
} }
if len(list.Items) == 0 { if len(list.Items) == 0 {
logFailure("no sources found in %s namespace", namespace) logger.Failuref("no sources found in %s namespace", namespace)
return nil return nil
} }
@ -61,16 +61,16 @@ func getSourceGitCmdRun(cmd *cobra.Command, args []string) error {
for _, condition := range source.Status.Conditions { for _, condition := range source.Status.Conditions {
if condition.Type == sourcev1.ReadyCondition { if condition.Type == sourcev1.ReadyCondition {
if condition.Status != corev1.ConditionFalse { if condition.Status != corev1.ConditionFalse {
logSuccess("%s last fetched revision: %s", source.GetName(), source.Status.Artifact.Revision) logger.Successf("%s last fetched revision: %s", source.GetName(), source.Status.Artifact.Revision)
} else { } else {
logFailure("%s %s", source.GetName(), condition.Message) logger.Failuref("%s %s", source.GetName(), condition.Message)
} }
isInitialized = true isInitialized = true
break break
} }
} }
if !isInitialized { if !isInitialized {
logFailure("%s is not ready", source.GetName()) logger.Failuref("%s is not ready", source.GetName())
} }
} }
return nil return nil

@ -81,7 +81,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
} }
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
logGenerate("generating manifests") logger.Generatef("generating manifests")
if kustomizePath == "" { if kustomizePath == "" {
err = genInstallManifests(installVersion, namespace, components, tmpDir) err = genInstallManifests(installVersion, namespace, components, tmpDir)
if err != nil { if err != nil {
@ -103,9 +103,9 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
fmt.Print(yaml) fmt.Print(yaml)
} }
} }
logSuccess("manifests build completed") logger.Successf("manifests build completed")
logAction("installing components in %s namespace", namespace) logger.Actionf("installing components in %s namespace", namespace)
applyOutput := ModeStderrOS applyOutput := ModeStderrOS
if verbose { if verbose {
applyOutput = ModeOS applyOutput = ModeOS
@ -121,24 +121,24 @@ func installCmdRun(cmd *cobra.Command, args []string) error {
} }
if installDryRun { if installDryRun {
logSuccess("install dry-run finished") logger.Successf("install dry-run finished")
return nil return nil
} else { } else {
logSuccess("install completed") logger.Successf("install completed")
} }
logWaiting("verifying installation") logger.Waitingf("verifying installation")
for _, deployment := range components { for _, deployment := range components {
command = fmt.Sprintf("kubectl -n %s rollout status deployment %s --timeout=%s", command = fmt.Sprintf("kubectl -n %s rollout status deployment %s --timeout=%s",
namespace, deployment, timeout.String()) namespace, deployment, timeout.String())
if _, err := utils.execCommand(ctx, applyOutput, command); err != nil { if _, err := utils.execCommand(ctx, applyOutput, command); err != nil {
return fmt.Errorf("install failed") return fmt.Errorf("install failed")
} else { } else {
logSuccess("%s ready", deployment) logger.Successf("%s ready", deployment)
} }
} }
logSuccess("install finished") logger.Successf("install finished")
return nil return nil
} }

@ -18,22 +18,24 @@ package main
import "fmt" import "fmt"
func logAction(format string, a ...interface{}) { type printLogger struct{}
func (l printLogger) Actionf(format string, a ...interface{}) {
fmt.Println(``, fmt.Sprintf(format, a...)) fmt.Println(``, fmt.Sprintf(format, a...))
} }
func logGenerate(format string, a ...interface{}) { func (l printLogger) Generatef(format string, a ...interface{}) {
fmt.Println(``, fmt.Sprintf(format, a...)) fmt.Println(``, fmt.Sprintf(format, a...))
} }
func logWaiting(format string, a ...interface{}) { func (l printLogger) Waitingf(format string, a ...interface{}) {
fmt.Println(``, fmt.Sprintf(format, a...)) fmt.Println(``, fmt.Sprintf(format, a...))
} }
func logSuccess(format string, a ...interface{}) { func (l printLogger) Successf(format string, a ...interface{}) {
fmt.Println(``, fmt.Sprintf(format, a...)) fmt.Println(``, fmt.Sprintf(format, a...))
} }
func logFailure(format string, a ...interface{}) { func (l printLogger) Failuref(format string, a ...interface{}) {
fmt.Println(``, fmt.Sprintf(format, a...)) fmt.Println(``, fmt.Sprintf(format, a...))
} }

@ -25,6 +25,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/cobra/doc" "github.com/spf13/cobra/doc"
_ "k8s.io/client-go/plugin/pkg/client/auth" _ "k8s.io/client-go/plugin/pkg/client/auth"
tklog "github.com/fluxcd/toolkit/pkg/log"
) )
var VERSION = "0.0.0-dev.0" var VERSION = "0.0.0-dev.0"
@ -98,7 +100,8 @@ var (
verbose bool verbose bool
components []string components []string
utils Utils utils Utils
pollInterval = 2 * time.Second pollInterval = 2 * time.Second
logger tklog.Logger = printLogger{}
) )
func init() { func init() {
@ -118,7 +121,7 @@ func main() {
generateDocs() generateDocs()
kubeconfigFlag() kubeconfigFlag()
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
logFailure("%v", err) logger.Failuref("%v", err)
os.Exit(1) os.Exit(1)
} }
} }

@ -66,20 +66,20 @@ func resumeKsCmdRun(cmd *cobra.Command, args []string) error {
return err return err
} }
logAction("resuming kustomization %s in %s namespace", name, namespace) logger.Actionf("resuming kustomization %s in %s namespace", name, namespace)
kustomization.Spec.Suspend = false kustomization.Spec.Suspend = false
if err := kubeClient.Update(ctx, &kustomization); err != nil { if err := kubeClient.Update(ctx, &kustomization); err != nil {
return err return err
} }
logSuccess("kustomization resumed") logger.Successf("kustomization resumed")
logWaiting("waiting for kustomization sync") logger.Waitingf("waiting for kustomization sync")
if err := wait.PollImmediate(pollInterval, timeout, if err := wait.PollImmediate(pollInterval, timeout,
isKustomizationResumed(ctx, kubeClient, name, namespace)); err != nil { isKustomizationResumed(ctx, kubeClient, name, namespace)); err != nil {
return err return err
} }
logSuccess("kustomization sync completed") logger.Successf("kustomization sync completed")
err = kubeClient.Get(ctx, namespacedName, &kustomization) err = kubeClient.Get(ctx, namespacedName, &kustomization)
if err != nil { if err != nil {
@ -87,7 +87,7 @@ func resumeKsCmdRun(cmd *cobra.Command, args []string) error {
} }
if kustomization.Status.LastAppliedRevision != "" { if kustomization.Status.LastAppliedRevision != "" {
logSuccess("applied revision %s", kustomization.Status.LastAppliedRevision) logger.Successf("applied revision %s", kustomization.Status.LastAppliedRevision)
} else { } else {
return fmt.Errorf("kustomization sync failed") return fmt.Errorf("kustomization sync failed")
} }

@ -60,12 +60,12 @@ func suspendKsCmdRun(cmd *cobra.Command, args []string) error {
return err return err
} }
logAction("suspending kustomization %s in %s namespace", name, namespace) logger.Actionf("suspending kustomization %s in %s namespace", name, namespace)
kustomization.Spec.Suspend = true kustomization.Spec.Suspend = true
if err := kubeClient.Update(ctx, &kustomization); err != nil { if err := kubeClient.Update(ctx, &kustomization); err != nil {
return err return err
} }
logSuccess("kustomization suspended") logger.Successf("kustomization suspended")
return nil return nil
} }

@ -83,7 +83,7 @@ func syncKsCmdRun(cmd *cobra.Command, args []string) error {
return err return err
} }
} else { } else {
logAction("annotating kustomization %s in %s namespace", name, namespace) logger.Actionf("annotating kustomization %s in %s namespace", name, namespace)
if kustomization.Annotations == nil { if kustomization.Annotations == nil {
kustomization.Annotations = map[string]string{ kustomization.Annotations = map[string]string{
kustomizev1.SyncAtAnnotation: time.Now().String(), kustomizev1.SyncAtAnnotation: time.Now().String(),
@ -94,16 +94,16 @@ func syncKsCmdRun(cmd *cobra.Command, args []string) error {
if err := kubeClient.Update(ctx, &kustomization); err != nil { if err := kubeClient.Update(ctx, &kustomization); err != nil {
return err return err
} }
logSuccess("kustomization annotated") logger.Successf("kustomization annotated")
} }
logWaiting("waiting for kustomization sync") logger.Waitingf("waiting for kustomization sync")
if err := wait.PollImmediate(pollInterval, timeout, if err := wait.PollImmediate(pollInterval, timeout,
isKustomizationReady(ctx, kubeClient, name, namespace)); err != nil { isKustomizationReady(ctx, kubeClient, name, namespace)); err != nil {
return err return err
} }
logSuccess("kustomization sync completed") logger.Successf("kustomization sync completed")
err = kubeClient.Get(ctx, namespacedName, &kustomization) err = kubeClient.Get(ctx, namespacedName, &kustomization)
if err != nil { if err != nil {
@ -111,7 +111,7 @@ func syncKsCmdRun(cmd *cobra.Command, args []string) error {
} }
if kustomization.Status.LastAppliedRevision != "" { if kustomization.Status.LastAppliedRevision != "" {
logSuccess("applied revision %s", kustomization.Status.LastAppliedRevision) logger.Successf("applied revision %s", kustomization.Status.LastAppliedRevision)
} else { } else {
return fmt.Errorf("kustomization sync failed") return fmt.Errorf("kustomization sync failed")
} }

@ -59,7 +59,7 @@ func syncSourceGitCmdRun(cmd *cobra.Command, args []string) error {
Name: name, Name: name,
} }
logAction("annotating source %s in %s namespace", name, namespace) logger.Actionf("annotating source %s in %s namespace", name, namespace)
var gitRepository sourcev1.GitRepository var gitRepository sourcev1.GitRepository
err = kubeClient.Get(ctx, namespacedName, &gitRepository) err = kubeClient.Get(ctx, namespacedName, &gitRepository)
if err != nil { if err != nil {
@ -76,15 +76,15 @@ func syncSourceGitCmdRun(cmd *cobra.Command, args []string) error {
if err := kubeClient.Update(ctx, &gitRepository); err != nil { if err := kubeClient.Update(ctx, &gitRepository); err != nil {
return err return err
} }
logSuccess("source annotated") logger.Successf("source annotated")
logWaiting("waiting for git sync") logger.Waitingf("waiting for git sync")
if err := wait.PollImmediate(pollInterval, timeout, if err := wait.PollImmediate(pollInterval, timeout,
isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil { isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil {
return err return err
} }
logSuccess("git sync completed") logger.Successf("git sync completed")
err = kubeClient.Get(ctx, namespacedName, &gitRepository) err = kubeClient.Get(ctx, namespacedName, &gitRepository)
if err != nil { if err != nil {
@ -92,7 +92,7 @@ func syncSourceGitCmdRun(cmd *cobra.Command, args []string) error {
} }
if gitRepository.Status.Artifact != nil { if gitRepository.Status.Artifact != nil {
logSuccess("fetched revision: %s", gitRepository.Status.Artifact.Revision) logger.Successf("fetched revision %s", gitRepository.Status.Artifact.Revision)
} else { } else {
return fmt.Errorf("git sync failed, artifact not found") return fmt.Errorf("git sync failed, artifact not found")
} }

@ -76,7 +76,7 @@ func uninstallCmdRun(cmd *cobra.Command, args []string) error {
} }
if uninstallKustomizations { if uninstallKustomizations {
logAction("uninstalling kustomizations") logger.Actionf("uninstalling kustomizations")
command := fmt.Sprintf("kubectl -n %s delete kustomizations --all --timeout=%s %s", command := fmt.Sprintf("kubectl -n %s delete kustomizations --all --timeout=%s %s",
namespace, timeout.String(), dryRun) namespace, timeout.String(), dryRun)
if _, err := utils.execCommand(ctx, ModeOS, command); err != nil { if _, err := utils.execCommand(ctx, ModeOS, command); err != nil {
@ -85,7 +85,7 @@ func uninstallCmdRun(cmd *cobra.Command, args []string) error {
// TODO: use the kustomizations snapshots to create a list of objects // TODO: use the kustomizations snapshots to create a list of objects
// that are subject to deletion and wait for all of them to be terminated // that are subject to deletion and wait for all of them to be terminated
logWaiting("waiting on GC") logger.Waitingf("waiting on GC")
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
} }
@ -94,13 +94,13 @@ func uninstallCmdRun(cmd *cobra.Command, args []string) error {
kinds += ",crds" kinds += ",crds"
} }
logAction("uninstalling components") logger.Actionf("uninstalling components")
command := fmt.Sprintf("kubectl delete %s -l app.kubernetes.io/instance=%s --timeout=%s %s", command := fmt.Sprintf("kubectl delete %s -l app.kubernetes.io/instance=%s --timeout=%s %s",
kinds, namespace, timeout.String(), dryRun) kinds, namespace, timeout.String(), dryRun)
if _, err := utils.execCommand(ctx, ModeOS, command); err != nil { if _, err := utils.execCommand(ctx, ModeOS, command); err != nil {
return fmt.Errorf("uninstall failed") return fmt.Errorf("uninstall failed")
} }
logSuccess("uninstall finished") logger.Successf("uninstall finished")
return nil return nil
} }

@ -0,0 +1,29 @@
/*
Copyright 2020 The Flux CD contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package log
type Logger interface {
// Actionf logs a formatted action message.
Actionf(format string, a ...interface{})
// Generatef logs a formatted generate message.
Generatef(format string, a ...interface{})
// Waitingf logs a formatted waiting message.
Waitingf(format string, a ...interface{})
// Waitingf logs a formatted success message.
Successf(format string, a ...interface{})
// Failuref logs a formatted failure message.
Failuref(format string, a ...interface{})
}
Loading…
Cancel
Save