-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate and add api for cluster status
Signed-off-by: Imtiaz Uddin <[email protected]>
- Loading branch information
Showing
8 changed files
with
418 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} |
Oops, something went wrong.