Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Oct 8, 2023
1 parent 34884d4 commit 2fb349a
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 144 deletions.
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
4 changes: 2 additions & 2 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) {
os.Exit(1)
}

if err := mgr.Add(manager.RunnableFunc(clusterstatusstorage.StartClusterStatusPuller(mgr))); err != nil {
if err := mgr.Add(manager.RunnableFunc(clusterstatusstorage.StartClusterStatusPuller(mgr.GetClient()))); err != nil {
setupLog.Error(err, "unable to set up cluster status puller")
os.Exit(1)
}
Expand All @@ -279,7 +279,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, cid, rbacAuthorizer)
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
90 changes: 37 additions & 53 deletions pkg/registry/meta/clusterstatus/fluxcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ package clusterstatus
import (
goctx "context"

appsv1 "k8s.io/api/apps/v1"
apps "k8s.io/api/apps/v1"
kerr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
)

const (
Expand All @@ -39,9 +37,9 @@ type FluxCDStatus struct {
Message string `json:"message,omitempty"`
}

func getFluxCDStatus(mgr manager.Manager) (FluxCDStatus, error) {
func getFluxCDStatus(kc client.Client) (FluxCDStatus, error) {
status := FluxCDStatus{}
if err := checkFluxCRDRegistered(mgr.GetConfig()); err != nil {
if err := checkFluxCRDRegistered(kc.RESTMapper()); err != nil {
if kerr.IsNotFound(err) || meta.IsNoMatchError(err) {
status.Installed = false
status.Message = "FluxCD CRDs HelmReleases and HelmRepositories are not registered"
Expand All @@ -50,73 +48,59 @@ func getFluxCDStatus(mgr manager.Manager) (FluxCDStatus, error) {
return status, err
}

kc := mgr.GetClient()

srcCtrl := appsv1.DeploymentList{}
err := kc.List(goctx.Background(), &srcCtrl, client.MatchingFields{
"metadata.name": "source-controller",
var deployments apps.DeploymentList
err := kc.List(goctx.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(srcCtrl.Items) == 0 {
if len(deployments.Items) == 0 {
status.Installed = false
status.Message = "Deployment 'source-controller' does not exist"
status.Message = "FluxCD deployments do 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 := appsv1.DeploymentList{}
err = kc.List(goctx.Background(), &helmCtrl, client.MatchingFields{
"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'"
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 {
if _, ok := podLabels[KeyACEManaged]; ok {
return true
}
return false
_, exists := podLabels[KeyACEManaged]
return exists
}

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

_, err = getResourceList(config, schema.GroupVersionResource{
Group: "source.toolkit.fluxcd.io",
Version: "v1beta1",
Resource: "helmrepositories",
})
if err != nil {
if _, err := mapper.RESTMappings(schema.GroupKind{
Group: "source.toolkit.fluxcd.io",
Kind: "HelmRepository",
}); err != nil {
return err
}
return nil
Expand Down
104 changes: 37 additions & 67 deletions pkg/registry/meta/clusterstatus/statuspuller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,16 @@ import (
"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"
kmcluster "kmodules.xyz/client-go/cluster"
"kmodules.xyz/resource-metadata/apis/meta/v1alpha1"
uiapi "kmodules.xyz/resource-metadata/apis/ui/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type clusterStatusResponse struct {
response *v1alpha1.ClusterStatusResponse
}

var clusterStatus *clusterStatusResponse
var clusterStatus *v1alpha1.ClusterStatusResponse

Check failure on line 35 in pkg/registry/meta/clusterstatus/statuspuller.go

View workflow job for this annotation

GitHub Actions / Build

var `clusterStatus` is unused (unused)

const (
pullingPeriod = time.Minute * 2
Expand All @@ -46,73 +41,52 @@ const (
MessageRequiredComponentsMissing = "One or more core components are not ready. Please, reconnect to update the components."
)

func init() {
clusterStatus = &clusterStatusResponse{
response: &v1alpha1.ClusterStatusResponse{},
}
}

func GetClusterStatus() *v1alpha1.ClusterStatusResponse {
return clusterStatus.response
}

func (cs *clusterStatusResponse) assignStatus(phase v1alpha1.ClusterPhase, Reason v1alpha1.ClusterPhaseReason, Message string) {
cs.response.Phase = phase
cs.response.Reason = Reason
cs.response.Message = Message
}

func StartClusterStatusPuller(mgr manager.Manager) func(ctx context.Context) error {
func StartClusterStatusPuller(kc client.Client) func(ctx context.Context) error {
return func(ctx context.Context) error {
go func() {
ticker := time.NewTicker(pullingPeriod)
defer ticker.Stop()

for {
cs := generateClusterStatus(mgr)
clusterStatus = cs
<-ticker.C
}
}()

return nil
for {
csr := generateClusterStatusResponse(kc)
clusterStatus = csr

time.Sleep(pullingPeriod)
}
}
}

func generateClusterStatus(mgr manager.Manager) *clusterStatusResponse {
csr := &clusterStatusResponse{
response: &v1alpha1.ClusterStatusResponse{},
}
client := mgr.GetClient()
func generateClusterStatusResponse(kc client.Client) *v1alpha1.ClusterStatusResponse {
var csr v1alpha1.ClusterStatusResponse
var err error

clusterManager := kmcluster.DetectClusterManager(client)
csr.response.ClusterManagers = clusterManager.Strings()
clusterManager := kmcluster.DetectClusterManager(kc)
csr.ClusterManagers = clusterManager.Strings()
if clusterManager.ManagedByOCMMulticlusterControlplane() {
csr.response.Phase = v1alpha1.ClusterPhaseActive
return csr
csr.Phase = v1alpha1.ClusterPhaseActive
return &csr
}

csr.response.ClusterAPI, err = kmcluster.DetectCAPICluster(client)
csr.ClusterAPI, err = kmcluster.DetectCAPICluster(kc)
if err != nil {
return csr
return &csr
}

ready, msg, err := checkClusterReadiness(mgr)
ready, msg, err := checkClusterReadiness(kc)
if err != nil {
csr.assignStatus(v1alpha1.ClusterPhaseInactive, v1alpha1.ClusterNotFound, err.Error())
return csr
csr.Phase = v1alpha1.ClusterPhaseInactive
csr.Reason = v1alpha1.ClusterNotFound
csr.Message = err.Error()
return &csr
} else if !ready {
csr.assignStatus(v1alpha1.ClusterPhaseNotReady, v1alpha1.MissingComponent, msg)
return csr
csr.Phase = v1alpha1.ClusterPhaseNotReady
csr.Reason = v1alpha1.MissingComponent
csr.Message = msg
return &csr
}
csr.response.Phase = v1alpha1.ClusterPhaseActive
csr.Phase = v1alpha1.ClusterPhaseActive

return csr
return &csr
}

func checkClusterReadiness(mgr manager.Manager) (bool, string, error) {
ready, err := isFluxCDReady(mgr)
func checkClusterReadiness(kc client.Client) (bool, string, error) {
ready, err := isFluxCDReady(kc)
if err != nil {
return false, "", err
}
Expand All @@ -121,7 +95,7 @@ func checkClusterReadiness(mgr manager.Manager) (bool, string, error) {
return false, MessageFluxCDMissing, nil
}

ready, err = areRequiredFeatureSetsReady(mgr)
ready, err = areRequiredFeatureSetsReady(kc)
if err != nil {
return false, "", err
}
Expand All @@ -131,8 +105,9 @@ func checkClusterReadiness(mgr manager.Manager) (bool, string, error) {
return true, "", nil
}

func areRequiredFeatureSetsReady(mgr manager.Manager) (bool, error) {
featureSets, err := getResourceList(mgr.GetConfig(), uiapi.SchemeGroupVersion.WithResource(uiapi.ResourceFeatureSets))
func areRequiredFeatureSetsReady(kc client.Client) (bool, error) {
var featureSets uiapi.FeatureSetList
err := kc.List(context.TODO(), &featureSets)
if err != nil {
return false, err
}
Expand All @@ -141,20 +116,15 @@ func areRequiredFeatureSetsReady(mgr manager.Manager) (bool, error) {
}

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) {
if len(fs.Spec.RequiredFeatures) > 0 && !pointer.Bool(fs.Status.Ready) {
return false, nil
}
}
return true, nil
}

func isFluxCDReady(mgr manager.Manager) (bool, error) {
status, err := getFluxCDStatus(mgr)
func isFluxCDReady(kc client.Client) (bool, error) {
status, err := getFluxCDStatus(kc)
if err != nil {
return false, err
}
Expand Down
11 changes: 3 additions & 8 deletions pkg/registry/meta/clusterstatus/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/authorization/authorizer"
"k8s.io/apiserver/pkg/registry/rest"
rsapi "kmodules.xyz/resource-metadata/apis/meta/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type Storage struct {
kc client.Client
clusterID string
a authorizer.Authorizer
convertor rest.TableConvertor
}

Expand All @@ -42,11 +39,9 @@ var (
_ rest.Creater = &Storage{}
)

func NewStorage(kc client.Client, clusterID string, a authorizer.Authorizer) *Storage {
func NewStorage(kc client.Client) *Storage {
return &Storage{
kc: kc,
clusterID: clusterID,
a: a,
kc: kc,
convertor: rest.NewDefaultTableConvertor(schema.GroupResource{
Group: rsapi.SchemeGroupVersion.Group,
Resource: rsapi.ResourceClusterStatuses,
Expand All @@ -70,7 +65,7 @@ func (r *Storage) Destroy() {}

func (r *Storage) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
in := obj.(*rsapi.ClusterStatus)
in.Response = GetClusterStatus()
in.Response = generateClusterStatusResponse(r.kc)

return in, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ const (
// +genclient:onlyVerbs=create
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type ClusterStatus struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
metav1.TypeMeta `json:",inline"`
// +optional
Response *ClusterStatusResponse `json:"response,omitempty"`
}
Expand Down
Loading

0 comments on commit 2fb349a

Please sign in to comment.