Skip to content

Commit

Permalink
create distribution method for cluster info
Browse files Browse the repository at this point in the history
Signed-off-by: Somtochi Onyekwere <[email protected]>
  • Loading branch information
somtochiama committed Dec 6, 2023
1 parent 5692ea9 commit 13eb7ec
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 54 deletions.
38 changes: 13 additions & 25 deletions cmd/flux/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func runCheckCmd(cmd *cobra.Command, args []string) error {

fluxCheck()

ctx := context.Background()
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()
kubeClient, err := utils.KubeClient(kubeconfigArgs, kubeclientOptions)
if err != nil {
return err
Expand All @@ -105,13 +106,13 @@ func runCheckCmd(cmd *cobra.Command, args []string) error {
return nil
}

logger.Actionf("checking cluster version")
logger.Actionf("checking version in cluster")
if !fluxClusterVersionCheck(ctx, kubeClient) {
checkFailed = true
}

logger.Actionf("checking controllers")
if !componentsCheck(ctx, cfg, kubeClient) {
if !componentsCheck(ctx, kubeClient) {
checkFailed = true
}

Expand Down Expand Up @@ -190,11 +191,8 @@ func kubernetesCheck(cfg *rest.Config, constraints []string) bool {
return true
}

func componentsCheck(ctx context.Context, kubeConfig *rest.Config, kubeClient client.Client) bool {
timeoutCtx, cancel := context.WithTimeout(ctx, rootArgs.timeout)
defer cancel()

statusChecker, err := status.NewStatusChecker(kubeConfig, checkArgs.pollInterval, rootArgs.timeout, logger)
func componentsCheck(ctx context.Context, kubeClient client.Client) bool {
statusChecker, err := status.NewStatusCheckerWithClient(kubeClient, checkArgs.pollInterval, rootArgs.timeout, logger)
if err != nil {
return false
}
Expand All @@ -203,7 +201,7 @@ func componentsCheck(ctx context.Context, kubeConfig *rest.Config, kubeClient cl
selector := client.MatchingLabels{manifestgen.PartOfLabelKey: manifestgen.PartOfLabelValue}
var list v1.DeploymentList
ns := *kubeconfigArgs.Namespace
if err := kubeClient.List(timeoutCtx, &list, client.InNamespace(ns), selector); err == nil {
if err := kubeClient.List(ctx, &list, client.InNamespace(ns), selector); err == nil {
if len(list.Items) == 0 {
logger.Failuref("no controllers found in the '%s' namespace with the label selector '%s=%s'",
ns, manifestgen.PartOfLabelKey, manifestgen.PartOfLabelValue)
Expand All @@ -225,18 +223,10 @@ func componentsCheck(ctx context.Context, kubeConfig *rest.Config, kubeClient cl
}

func crdsCheck(ctx context.Context, kubeClient client.Client) bool {
timeoutCtx, cancel := context.WithTimeout(ctx, rootArgs.timeout)
defer cancel()

kubeClient, err := utils.KubeClient(kubeconfigArgs, kubeclientOptions)
if err != nil {
return false
}

ok := true
selector := client.MatchingLabels{manifestgen.PartOfLabelKey: manifestgen.PartOfLabelValue}
var list apiextensionsv1.CustomResourceDefinitionList
if err := kubeClient.List(timeoutCtx, &list, client.InNamespace(*kubeconfigArgs.Namespace), selector); err == nil {
if err := kubeClient.List(ctx, &list, client.InNamespace(*kubeconfigArgs.Namespace), selector); err == nil {
if len(list.Items) == 0 {
logger.Failuref("no crds found with the label selector '%s=%s'",
manifestgen.PartOfLabelKey, manifestgen.PartOfLabelValue)
Expand All @@ -257,17 +247,15 @@ func crdsCheck(ctx context.Context, kubeClient client.Client) bool {
}

func fluxClusterVersionCheck(ctx context.Context, kubeClient client.Client) bool {
timeoutCtx, cancel := context.WithTimeout(ctx, rootArgs.timeout)
defer cancel()

distribution, bootstrapped, err := getFluxDistribution(timeoutCtx, kubeClient)
clusterInfo, err := getFluxClusterInfo(ctx, kubeClient)
if err != nil {
logger.Failuref("checking failed: %s", err.Error())
return false
}

if distribution != "" {
logger.Successf("distribution: %s", distribution)
if clusterInfo.distribution() != "" {
logger.Successf("distribution: %s", clusterInfo.distribution())
}
logger.Successf("bootstrapped: %t", bootstrapped)
logger.Successf("bootstrapped: %t", clusterInfo.bootstrapped)
return true
}
23 changes: 8 additions & 15 deletions cmd/flux/cluster_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import (

"github.com/manifoldco/promptui"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/fluxcd/flux2/v2/pkg/manifestgen"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
sourcev1 "github.com/fluxcd/source-controller/api/v1"

"github.com/fluxcd/flux2/v2/pkg/manifestgen"
)

// bootstrapLabels are labels put on a resource by kustomize-controller. These labels on the CRD indicates
Expand Down Expand Up @@ -82,7 +82,7 @@ func getFluxClusterInfo(ctx context.Context, c client.Client) (fluxClusterInfo,
info.bootstrapped = true
}

// the `app.kubernetes.io` label is not set by flux but might be set by other
// the `app.kubernetes.io/managed-by` label is not set by flux but might be set by other
// tools used to install Flux e.g Helm.
if manager, ok := crdMetadata.Labels["app.kubernetes.io/managed-by"]; ok {
info.managedBy = manager
Expand Down Expand Up @@ -113,19 +113,12 @@ func confirmFluxInstallOverride(info fluxClusterInfo) error {
return err
}

func getFluxDistribution(ctx context.Context, kubeClient client.Client) (string, bool, error) {
clusterInfo, err := getFluxClusterInfo(ctx, kubeClient)
if err != nil {
if !errors.IsNotFound(err) {
return "", false, fmt.Errorf("cluster info unavailable: %w", err)
}
}

distribution := clusterInfo.version
if clusterInfo.partOf != "" {
distribution = fmt.Sprintf("%s-%s", clusterInfo.partOf, clusterInfo.version)
func (info fluxClusterInfo) distribution() string {
distribution := info.version
if info.partOf != "" {
distribution = fmt.Sprintf("%s-%s", info.partOf, info.version)
}
return distribution, clusterInfo.bootstrapped, nil
return distribution
}

func installManagedByFlux(manager string) bool {
Expand Down
15 changes: 8 additions & 7 deletions cmd/flux/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/google/go-containerregistry/pkg/name"
"github.com/spf13/cobra"
v1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml/goyaml.v2"

Expand Down Expand Up @@ -55,7 +56,7 @@ type versionFlags struct {

var versionArgs versionFlags

type VersionInfo struct {
type versionInfo struct {
Flux string `yaml:"flux"`
Distribution string `yaml:"distribution,omitempty"`
Controller map[string]string `yaml:"controller,inline"`
Expand All @@ -80,7 +81,7 @@ func versionCmdRun(cmd *cobra.Command, args []string) error {
// VersionInfo struct is used for yaml because we care about the order.
// Without this `distribution` is printed before `flux`.
// Unfortunately, encoding/json doesn't support the inline tag, so the struct can't be used for json.
yamlInfo := &VersionInfo{
yamlInfo := &versionInfo{
Controller: map[string]string{},
}
info := map[string]string{}
Expand All @@ -93,13 +94,13 @@ func versionCmdRun(cmd *cobra.Command, args []string) error {
return err
}

distribution, _, err := getFluxDistribution(ctx, kubeClient)
if err != nil {
clusterInfo, err := getFluxClusterInfo(ctx, kubeClient)
if err != nil && !errors.IsNotFound(err) {
return err
}
if distribution != "" {
info["distribution"] = distribution
yamlInfo.Distribution = distribution
if clusterInfo.distribution() != "" {
info["distribution"] = clusterInfo.distribution()
yamlInfo.Distribution = clusterInfo.distribution()
}

selector := client.MatchingLabels{manifestgen.PartOfLabelKey: manifestgen.PartOfLabelValue}
Expand Down
19 changes: 12 additions & 7 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ type StatusChecker struct {
logger log.Logger
}

func NewStatusCheckerWithClient(c client.Client, pollInterval time.Duration, timeout time.Duration, log log.Logger) (*StatusChecker, error) {
return &StatusChecker{
pollInterval: pollInterval,
timeout: timeout,
client: c,
statusPoller: polling.NewStatusPoller(c, c.RESTMapper(), polling.Options{}),
logger: log,
}, nil
}


func NewStatusChecker(kubeConfig *rest.Config, pollInterval time.Duration, timeout time.Duration, log log.Logger) (*StatusChecker, error) {
restMapper, err := runtimeclient.NewDynamicRESTMapper(kubeConfig)
if err != nil {
Expand All @@ -55,13 +66,7 @@ func NewStatusChecker(kubeConfig *rest.Config, pollInterval time.Duration, timeo
return nil, err
}

return &StatusChecker{
pollInterval: pollInterval,
timeout: timeout,
client: c,
statusPoller: polling.NewStatusPoller(c, restMapper, polling.Options{}),
logger: log,
}, nil
return NewStatusCheckerWithClient(c, pollInterval, timeout, log)
}

func (sc *StatusChecker) Assess(identifiers ...object.ObjMetadata) error {
Expand Down

0 comments on commit 13eb7ec

Please sign in to comment.