Skip to content

Commit

Permalink
Generate and add api for cluster status
Browse files Browse the repository at this point in the history
Signed-off-by: Imtiaz Uddin <[email protected]>
  • Loading branch information
Imtiaz246 committed Oct 5, 2023
1 parent 66a9c20 commit 17646be
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 0 deletions.
31 changes: 31 additions & 0 deletions apis/cost/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions apis/identity/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions apis/policy/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
identityv1alpha1 "kubeops.dev/ui-server/apis/identity/v1alpha1"
policyinstall "kubeops.dev/ui-server/apis/policy/install"
policyapi "kubeops.dev/ui-server/apis/policy/v1alpha1"
"kubeops.dev/ui-server/pkg/clusterstatus"
projectquotacontroller "kubeops.dev/ui-server/pkg/controllers/projectquota"
"kubeops.dev/ui-server/pkg/graph"
"kubeops.dev/ui-server/pkg/metricshandler"
Expand Down Expand Up @@ -254,6 +255,11 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) {
os.Exit(1)
}

if err := mgr.Add(manager.RunnableFunc(clusterstatus.StartClusterStatusPuller(mgr))); err != nil {
setupLog.Error(err, "unable to set up cluster status puller")
os.Exit(1)
}

s := &UIServer{
GenericAPIServer: genericServer,
Manager: mgr,
Expand Down
124 changes: 124 additions & 0 deletions pkg/clusterstatus/fluxcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
Copyright AppsCode Inc. and 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 clusterstatus

import (
goctx "context"

core "k8s.io/api/core/v1"
kerr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

const (
KeyACEManaged = "byte.builders/managed"
)

type FluxCDStatus struct {
Installed bool `json:"installed"`
Ready bool `json:"ready"`
Managed bool `json:"managed"`
Message string `json:"message,omitempty"`
}

func getFluxCDStatus(config *rest.Config) (FluxCDStatus, error) {
status := FluxCDStatus{}
if err := checkFluxCRDRegistered(config); err != nil {
if kerr.IsNotFound(err) || meta.IsNoMatchError(err) {
status.Installed = false
status.Message = "FluxCD CRDs HelmReleases and HelmRepositories are not registered"
return status, nil
}
return status, err
}

kc, err := kubernetes.NewForConfig(config)
if err != nil {
return status, err
}

srcCtrl, err := kc.AppsV1().Deployments(core.NamespaceAll).List(goctx.Background(), metav1.ListOptions{
FieldSelector: "metadata.name=source-controller",
})
if err != nil {
return status, err
}
if len(srcCtrl.Items) == 0 {
status.Installed = false
status.Message = "Deployment 'source-controller' does not exist"
return status, nil
}
status.Installed = true
status.Managed = isFluxCDManaged(srcCtrl.Items[0].Spec.Template.Labels)

if srcCtrl.Items[0].Status.ReadyReplicas == 0 {
status.Ready = false
status.Message = "No ready replica found for deployment 'source-controller'"
return status, nil
}

helmCtrl, err := kc.AppsV1().Deployments(core.NamespaceAll).List(goctx.Background(), metav1.ListOptions{
FieldSelector: "metadata.name=helm-controller",
})
if err != nil {
return status, err
}
if len(helmCtrl.Items) == 0 {
status.Ready = false
status.Message = "Deployment 'helm-controller' does not exist"
return status, nil
}
if helmCtrl.Items[0].Status.ReadyReplicas == 0 {
status.Ready = false
status.Message = "No ready replica found for deployment 'helm-controller'"
}
status.Ready = true

return status, nil
}

func isFluxCDManaged(podLabels map[string]string) bool {
if _, ok := podLabels[KeyACEManaged]; ok {
return true
}
return false
}

func checkFluxCRDRegistered(config *rest.Config) error {
_, err := getResourceList(config, schema.GroupVersionResource{
Group: "helm.toolkit.fluxcd.io",
Version: "v2beta1",
Resource: "helmreleases",
})
if err != nil {
return err
}

_, err = getResourceList(config, schema.GroupVersionResource{
Group: "source.toolkit.fluxcd.io",
Version: "v1beta1",
Resource: "helmrepositories",
})
if err != nil {
return err
}
return nil
}
93 changes: 93 additions & 0 deletions pkg/clusterstatus/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright AppsCode Inc. and 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 clusterstatus

import (
goctx "context"

"gomodules.xyz/pointer"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
uiapi "kmodules.xyz/resource-metadata/apis/ui/v1alpha1"
)

const (
MessageFluxCDMissing = "FluxCD is not installed or not ready. Please, reconnect to install/update the component."
MessageRequiredComponentsMissing = "One or more core components are not ready. Please, reconnect to update the components."
)

func checkClusterReadiness(config *rest.Config) (bool, string, error) {
ready, err := isFluxCDReady(config)
if err != nil {
return false, "", err
}

if !ready {
return false, MessageFluxCDMissing, nil
}

ready, err = areRequiredFeatureSetsReady(config)
if err != nil {
return false, "", err
}
if !ready {
return false, MessageRequiredComponentsMissing, nil
}
return true, "", nil
}

func areRequiredFeatureSetsReady(config *rest.Config) (bool, error) {
featureSets, err := getResourceList(config, uiapi.SchemeGroupVersion.WithResource(uiapi.ResourceFeatureSets))
if err != nil {
return false, err
}
if len(featureSets.Items) == 0 {
return false, nil
}

for _, fs := range featureSets.Items {
featureSet := uiapi.FeatureSet{}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(fs.UnstructuredContent(), &featureSet)
if err != nil {
return false, err
}
if len(featureSet.Spec.RequiredFeatures) > 0 && !pointer.Bool(featureSet.Status.Ready) {
return false, nil
}
}
return true, nil
}

func isFluxCDReady(config *rest.Config) (bool, error) {
status, err := getFluxCDStatus(config)
if err != nil {
return false, err
}
return status.Ready, nil
}

func getResourceList(config *rest.Config, gvr schema.GroupVersionResource) (*unstructured.UnstructuredList, error) {
dc, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
return dc.Resource(gvr).List(goctx.Background(), metav1.ListOptions{})
}
Loading

0 comments on commit 17646be

Please sign in to comment.