diff --git a/cmd/flux/get_helmrelease.go b/cmd/flux/get_helmrelease.go index 6577105f..89828acd 100644 --- a/cmd/flux/get_helmrelease.go +++ b/cmd/flux/get_helmrelease.go @@ -17,20 +17,11 @@ limitations under the License. package main import ( - "context" - "os" "strconv" "strings" - "github.com/fluxcd/flux2/internal/utils" - "github.com/fluxcd/pkg/apis/meta" - - "github.com/spf13/cobra" - apimeta "k8s.io/apimachinery/pkg/api/meta" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" - helmv2 "github.com/fluxcd/helm-controller/api/v2beta1" + "github.com/spf13/cobra" ) var getHelmReleaseCmd = &cobra.Command{ @@ -41,66 +32,28 @@ var getHelmReleaseCmd = &cobra.Command{ Example: ` # List all Helm releases and their status flux get helmreleases `, - RunE: getHelmReleaseCmdRun, + RunE: getCommand{ + apiType: helmReleaseType, + list: &helmReleaseListAdapter{&helmv2.HelmReleaseList{}}, + }.run, } func init() { getCmd.AddCommand(getHelmReleaseCmd) } -func getHelmReleaseCmdRun(cmd *cobra.Command, args []string) error { - ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) - defer cancel() - - kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext) - if err != nil { - return err - } - - var listOpts []client.ListOption - if !getArgs.allNamespaces { - listOpts = append(listOpts, client.InNamespace(rootArgs.namespace)) - } - var list helmv2.HelmReleaseList - err = kubeClient.List(ctx, &list, listOpts...) - if err != nil { - return err - } - - if len(list.Items) == 0 { - logger.Failuref("no releases found in %s namespace", rootArgs.namespace) - return nil - } +func (a helmReleaseListAdapter) summariseItem(i int, includeNamespace bool) []string { + item := a.Items[i] + revision := item.Status.LastAppliedRevision + status, msg := statusAndMessage(item.Status.Conditions) + return append(nameColumns(&item, includeNamespace), + status, msg, revision, strings.Title(strconv.FormatBool(item.Spec.Suspend))) +} - header := []string{"Name", "Ready", "Message", "Revision", "Suspended"} - if getArgs.allNamespaces { - header = append([]string{"Namespace"}, header...) - } - var rows [][]string - for _, helmRelease := range list.Items { - row := []string{} - if c := apimeta.FindStatusCondition(helmRelease.Status.Conditions, meta.ReadyCondition); c != nil { - row = []string{ - helmRelease.GetName(), - string(c.Status), - c.Message, - helmRelease.Status.LastAppliedRevision, - strings.Title(strconv.FormatBool(helmRelease.Spec.Suspend)), - } - } else { - row = []string{ - helmRelease.GetName(), - string(metav1.ConditionFalse), - "waiting to be reconciled", - helmRelease.Status.LastAppliedRevision, - strings.Title(strconv.FormatBool(helmRelease.Spec.Suspend)), - } - } - if getArgs.allNamespaces { - row = append([]string{helmRelease.Namespace}, row...) - } - rows = append(rows, row) +func (a helmReleaseListAdapter) headers(includeNamespace bool) []string { + headers := []string{"Name", "Ready", "Message", "Revision", "Suspended"} + if includeNamespace { + headers = append([]string{"Namespace"}, headers...) } - utils.PrintTable(os.Stdout, header, rows) - return nil + return headers } diff --git a/cmd/flux/get_kustomization.go b/cmd/flux/get_kustomization.go index d0c6553d..58623a8a 100644 --- a/cmd/flux/get_kustomization.go +++ b/cmd/flux/get_kustomization.go @@ -17,19 +17,11 @@ limitations under the License. package main import ( - "context" - "os" "strconv" "strings" - "github.com/fluxcd/flux2/internal/utils" - "github.com/fluxcd/pkg/apis/meta" - kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1" "github.com/spf13/cobra" - apimeta "k8s.io/apimachinery/pkg/api/meta" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" ) var getKsCmd = &cobra.Command{ @@ -40,66 +32,28 @@ var getKsCmd = &cobra.Command{ Example: ` # List all kustomizations and their status flux get kustomizations `, - RunE: getKsCmdRun, + RunE: getCommand{ + apiType: kustomizationType, + list: &kustomizationListAdapter{&kustomizev1.KustomizationList{}}, + }.run, } func init() { getCmd.AddCommand(getKsCmd) } -func getKsCmdRun(cmd *cobra.Command, args []string) error { - ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) - defer cancel() - - kubeClient, err := utils.KubeClient(rootArgs.kubeconfig, rootArgs.kubecontext) - if err != nil { - return err - } - - var listOpts []client.ListOption - if !getArgs.allNamespaces { - listOpts = append(listOpts, client.InNamespace(rootArgs.namespace)) - } - var list kustomizev1.KustomizationList - err = kubeClient.List(ctx, &list, listOpts...) - if err != nil { - return err - } - - if len(list.Items) == 0 { - logger.Failuref("no kustomizations found in %s namespace", rootArgs.namespace) - return nil - } +func (a kustomizationListAdapter) summariseItem(i int, includeNamespace bool) []string { + item := a.Items[i] + revision := item.Status.LastAppliedRevision + status, msg := statusAndMessage(item.Status.Conditions) + return append(nameColumns(&item, includeNamespace), + status, msg, revision, strings.Title(strconv.FormatBool(item.Spec.Suspend))) +} - header := []string{"Name", "Ready", "Message", "Revision", "Suspended"} - if getArgs.allNamespaces { - header = append([]string{"Namespace"}, header...) - } - var rows [][]string - for _, kustomization := range list.Items { - row := []string{} - if c := apimeta.FindStatusCondition(kustomization.Status.Conditions, meta.ReadyCondition); c != nil { - row = []string{ - kustomization.GetName(), - string(c.Status), - c.Message, - kustomization.Status.LastAppliedRevision, - strings.Title(strconv.FormatBool(kustomization.Spec.Suspend)), - } - } else { - row = []string{ - kustomization.GetName(), - string(metav1.ConditionFalse), - "waiting to be reconciled", - kustomization.Status.LastAppliedRevision, - strings.Title(strconv.FormatBool(kustomization.Spec.Suspend)), - } - } - if getArgs.allNamespaces { - row = append([]string{kustomization.Namespace}, row...) - } - rows = append(rows, row) +func (a kustomizationListAdapter) headers(includeNamespace bool) []string { + headers := []string{"Name", "Ready", "Message", "Revision", "Suspended"} + if includeNamespace { + headers = append([]string{"Namespace"}, headers...) } - utils.PrintTable(os.Stdout, header, rows) - return nil + return headers } diff --git a/cmd/flux/helmrelease.go b/cmd/flux/helmrelease.go new file mode 100644 index 00000000..109dc3f6 --- /dev/null +++ b/cmd/flux/helmrelease.go @@ -0,0 +1,51 @@ +/* +Copyright 2021 The Flux authors + +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 main + +import ( + helmv2 "github.com/fluxcd/helm-controller/api/v2beta1" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +// helmv2.HelmRelease + +var helmReleaseType = apiType{ + kind: helmv2.HelmReleaseKind, + humanKind: "helmreleases", +} + +type helmReleaseAdapter struct { + *helmv2.HelmRelease +} + +func (h helmReleaseAdapter) asClientObject() client.Object { + return h.HelmRelease +} + +// helmv2.HelmReleaseList + +type helmReleaseListAdapter struct { + *helmv2.HelmReleaseList +} + +func (h helmReleaseListAdapter) asClientList() client.ObjectList { + return h.HelmReleaseList +} + +func (h helmReleaseListAdapter) len() int { + return len(h.HelmReleaseList.Items) +} diff --git a/cmd/flux/kustomization.go b/cmd/flux/kustomization.go new file mode 100644 index 00000000..49cdbace --- /dev/null +++ b/cmd/flux/kustomization.go @@ -0,0 +1,51 @@ +/* +Copyright 2020 The Flux authors + +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 main + +import ( + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +// kustomizev1.Kustomization + +var kustomizationType = apiType{ + kind: kustomizev1.KustomizationKind, + humanKind: "kustomizations", +} + +type kustomizationAdapter struct { + *kustomizev1.Kustomization +} + +func (a kustomizationAdapter) asClientObject() client.Object { + return a.Kustomization +} + +// kustomizev1.KustomizationList + +type kustomizationListAdapter struct { + *kustomizev1.KustomizationList +} + +func (a kustomizationListAdapter) asClientList() client.ObjectList { + return a.KustomizationList +} + +func (a kustomizationListAdapter) len() int { + return len(a.KustomizationList.Items) +}