Skip to content

Commit

Permalink
Generate and add api for cluster status (#263)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
Signed-off-by: Imtiaz Uddin <[email protected]>
Co-authored-by: Tamal Saha <[email protected]>
  • Loading branch information
Imtiaz246 and tamalsaha authored Oct 8, 2023
1 parent de03420 commit 2f5d3ac
Show file tree
Hide file tree
Showing 19 changed files with 425 additions and 15 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.

2 changes: 2 additions & 0 deletions artifacts/clusterstatus/cs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apiVersion: meta.k8s.appscode.com/v1alpha1
kind: ClusterStatus
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (
kmodules.xyz/custom-resources v0.25.2
kmodules.xyz/go-containerregistry v0.0.11
kmodules.xyz/monitoring-agent-api v0.25.4
kmodules.xyz/resource-metadata v0.17.25-0.20231006102558-311a1a2eaa10
kmodules.xyz/resource-metadata v0.17.25-0.20231008042713-ef9e7bdbcfb9
kmodules.xyz/resource-metrics v0.25.3
kmodules.xyz/sets v0.25.0
kubeops.dev/scanner v0.0.15-0.20230929084557-c991a6514ff9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2141,8 +2141,8 @@ kmodules.xyz/monitoring-agent-api v0.25.4 h1:OwkvtV23QhUy3f7o9lxPWaAW3jiJMGAMPPR
kmodules.xyz/monitoring-agent-api v0.25.4/go.mod h1:3LhrLDGQKQXhxYcjA/WNaO4HPpopYQzOutsEp2i3008=
kmodules.xyz/offshoot-api v0.25.4 h1:IjJNvkphcdYUG8XO/pBwXpuP8W+jxAWJZ3yH8vgI/as=
kmodules.xyz/offshoot-api v0.25.4/go.mod h1:PUk4EuJFhhyQykCflHj7EgXcljGIqs9vi0IN0RpxtY4=
kmodules.xyz/resource-metadata v0.17.25-0.20231006102558-311a1a2eaa10 h1:D1HcIuYxmHG44nQY6r9dm9qKU+pKtPUDiekJaRjbu+o=
kmodules.xyz/resource-metadata v0.17.25-0.20231006102558-311a1a2eaa10/go.mod h1:tyLxzAVkhlL3/jFdcQcX1RZ8i8h9bs+AJur5RcuaW40=
kmodules.xyz/resource-metadata v0.17.25-0.20231008042713-ef9e7bdbcfb9 h1:CNuwviXfHRSi8m8pgYc5CVLvd3jTghDRKgsixbE1ei8=
kmodules.xyz/resource-metadata v0.17.25-0.20231008042713-ef9e7bdbcfb9/go.mod h1:tyLxzAVkhlL3/jFdcQcX1RZ8i8h9bs+AJur5RcuaW40=
kmodules.xyz/resource-metrics v0.25.3 h1:g9EjNfYRrUSnbA4r+bUQefQ5Ban6I6rpKjnB3ER+Yew=
kmodules.xyz/resource-metrics v0.25.3/go.mod h1:H7YLdUQJXUSzf5cNI4IYWU4Wsmrua/jpw7gqDnE3BwM=
kmodules.xyz/sets v0.25.0 h1:belY/3trp/M/CKc1TEteA40jb2uCIdwKHhjpvrIxG+8=
Expand Down
2 changes: 2 additions & 0 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
coststorage "kubeops.dev/ui-server/pkg/registry/cost/reports"
whoamistorage "kubeops.dev/ui-server/pkg/registry/identity/whoami"
"kubeops.dev/ui-server/pkg/registry/meta/chartpresetquery"
clusterstatusstorage "kubeops.dev/ui-server/pkg/registry/meta/clusterstatus"
"kubeops.dev/ui-server/pkg/registry/meta/render"
"kubeops.dev/ui-server/pkg/registry/meta/renderdashboard"
"kubeops.dev/ui-server/pkg/registry/meta/rendermenu"
Expand Down Expand Up @@ -273,6 +274,7 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) {

v1alpha1storage := map[string]rest.Storage{}
v1alpha1storage[rsapi.ResourceChartPresetQueries] = chartpresetquery.NewStorage(ctrlClient)
v1alpha1storage[rsapi.ResourceClusterStatuses] = clusterstatusstorage.NewStorage(ctrlClient)
v1alpha1storage[rsapi.ResourceRenderDashboards] = renderdashboard.NewStorage(ctrlClient, oc)
v1alpha1storage[rsapi.ResourceRenderRawGraphs] = renderrawgraph.NewStorage(ctrlClient)
v1alpha1storage[rsapi.ResourceRenders] = render.NewStorage(ctrlClient, oc, rbacAuthorizer)
Expand Down
1 change: 1 addition & 0 deletions pkg/cmds/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (o *UIServerOptions) Config() (*apiserver.Config, error) {

fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, "usermenus"),
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, rsapi.ResourceChartPresetQueries),
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, rsapi.ResourceClusterStatuses),
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, rsapi.ResourceMenus),
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, rsapi.ResourceRenderDashboards),
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, rsapi.ResourceRenderMenus),
Expand Down
107 changes: 107 additions & 0 deletions pkg/registry/meta/clusterstatus/fluxcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
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 (
"context"

apps "k8s.io/api/apps/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
)

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(kc client.Client) (FluxCDStatus, error) {
status := FluxCDStatus{}
if err := checkFluxCRDRegistered(kc.RESTMapper()); err != nil {
if apierrors.IsNotFound(err) || meta.IsNoMatchError(err) {
status.Installed = false
status.Message = "FluxCD CRDs HelmReleases and HelmRepositories are not registered"
return status, nil
}
return status, err
}

var deployments apps.DeploymentList
err := kc.List(context.Background(), &deployments, client.MatchingLabels{
"app.kubernetes.io/instance": "flux-system",
"app.kubernetes.io/part-of": "flux",
"control-plane": "controller",
})
if err != nil {
return status, err
}
if len(deployments.Items) == 0 {
status.Installed = false
status.Message = "FluxCD deployments do not exist"
return status, nil
}

for _, deploy := range deployments.Items {
if deploy.Name == "source-controller" {
status.Installed = true
status.Managed = isFluxCDManaged(deploy.Spec.Template.Labels)

if deploy.Status.ReadyReplicas == 0 {
status.Ready = false
status.Message = "No ready replica found for deployment 'source-controller'"
return status, nil
}
} else if deploy.Name == "helm-controller" {
if deploy.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 {
_, exists := podLabels[KeyACEManaged]
return exists
}

func checkFluxCRDRegistered(mapper meta.RESTMapper) error {
if _, err := mapper.RESTMappings(schema.GroupKind{
Group: "helm.toolkit.fluxcd.io",
Kind: "HelmRelease",
}); err != nil {
return err
}
if _, err := mapper.RESTMappings(schema.GroupKind{
Group: "source.toolkit.fluxcd.io",
Kind: "HelmRepository",
}); err != nil {
return err
}
return nil
}
Loading

0 comments on commit 2f5d3ac

Please sign in to comment.