Skip to content

Commit

Permalink
Add clusterClaim controller
Browse files Browse the repository at this point in the history
Signed-off-by: Rokibul Hasan <[email protected]>
  • Loading branch information
RokibulHasan7 committed Nov 11, 2024
1 parent 9a86127 commit 1c5da0c
Show file tree
Hide file tree
Showing 15 changed files with 1,356 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require (
kubeops.dev/falco-ui-server v0.0.4
kubeops.dev/scanner v0.0.18
kubepack.dev/lib-helm v0.29.13
open-cluster-management.io/api v0.15.0
sigs.k8s.io/cli-utils v0.37.2
sigs.k8s.io/controller-runtime v0.18.4
sigs.k8s.io/yaml v1.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,8 @@ kubevault.dev/apimachinery v0.18.3 h1:Bq180AGBYnRXXNWbJ6Zg82+8/3M1Y8WYPez32uTry8
kubevault.dev/apimachinery v0.18.3/go.mod h1:b9uUVFx3a3ThDziL2J2O4xQL+muY1/pGavAhDdJC99E=
moul.io/http2curl/v2 v2.3.1-0.20221024080105-10c404f653f7 h1:NykkTlRB+X40z86cLHdEmuoTxhNKhQebLT379b1EumA=
moul.io/http2curl/v2 v2.3.1-0.20221024080105-10c404f653f7/go.mod h1:RW4hyBjTWSYDOxapodpNEtX0g5Eb16sxklBqmd2RHcE=
open-cluster-management.io/api v0.15.0 h1:lRee1KOlGHZb2scTA7ff9E9Fxt2hJc7jpkHnaCbvkOU=
open-cluster-management.io/api v0.15.0/go.mod h1:9erZEWEn4bEqh0nIX2wA7f/s3KCuFycQdBrPrRzi0QM=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.29.0 h1:/U5vjBbQn3RChhv7P11uhYvCSm5G2GaIi5AIGBS6r4c=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.29.0/go.mod h1:z7+wmGM2dfIiLRfrC6jb5kV2Mq/sK1ZP303cxzkV5Y4=
Expand Down
11 changes: 11 additions & 0 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
licenseapi "kubeops.dev/ui-server/apis/offline/v1alpha1"
policyinstall "kubeops.dev/ui-server/apis/policy/install"
policyapi "kubeops.dev/ui-server/apis/policy/v1alpha1"
clusterclaimcontroller "kubeops.dev/ui-server/pkg/controllers/clusterclaim"
clustermetacontroller "kubeops.dev/ui-server/pkg/controllers/clustermetadata"
projectquotacontroller "kubeops.dev/ui-server/pkg/controllers/projectquota"
"kubeops.dev/ui-server/pkg/graph"
Expand Down Expand Up @@ -107,6 +108,7 @@ import (
uiinstall "kmodules.xyz/resource-metadata/apis/ui/install"
uiapi "kmodules.xyz/resource-metadata/apis/ui/v1alpha1"
identitylib "kmodules.xyz/resource-metadata/pkg/identity"
clusterv1alpha1 "open-cluster-management.io/api/cluster/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down Expand Up @@ -140,6 +142,7 @@ func init() {
utilruntime.Must(fluxsrc.AddToScheme(Scheme))
utilruntime.Must(monitoringv1.AddToScheme(Scheme))
utilruntime.Must(falco.AddToScheme(Scheme))
utilruntime.Must(clusterv1alpha1.Install(Scheme))

// we need to add the options to empty v1
// TODO fix the server code to avoid this
Expand Down Expand Up @@ -298,6 +301,14 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) {
}
}

if clustermeta.DetectClusterManager(mgr.GetClient()).ManagedByOCMSpoke() {
err = clusterclaimcontroller.NewReconciler(mgr.GetClient()).SetupWithManager(mgr)
if err != nil {
klog.Error(err, "unable to create controller", "controller", "ClusterClaim")
os.Exit(1)
}
}

s := &UIServer{
GenericAPIServer: genericServer,
Manager: mgr,
Expand Down
103 changes: 103 additions & 0 deletions pkg/controllers/clusterclaim/clusterclaim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
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 clusterclaim

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
cu "kmodules.xyz/client-go/client"
uiapi "kmodules.xyz/resource-metadata/apis/ui/v1alpha1"
clusterv1alpha1 "open-cluster-management.io/api/cluster/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/yaml"
)

const FeatureClusterClaim = "features.appscode.com"

type ClusterClaimReconciler struct {
kc client.Client
}

var _ reconcile.Reconciler = &ClusterClaimReconciler{}

func NewReconciler(kc client.Client) *ClusterClaimReconciler {
return &ClusterClaimReconciler{
kc: kc,
}
}

func (r *ClusterClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
var err error
var featureList uiapi.FeatureList
if err = r.kc.List(ctx, &featureList); err != nil {
return ctrl.Result{}, err
}

var enabledFeatures []string
var notManagedFeatures []string
var disabledFeatures []string

for _, feature := range featureList.Items {
if feature.Status.Enabled == nil {
return ctrl.Result{}, fmt.Errorf("feature is not reconciled yet")
}
if ptr.Deref(feature.Status.Enabled, false) {
enabledFeatures = append(enabledFeatures, feature.Name)
if !ptr.Deref(feature.Status.Managed, false) {
notManagedFeatures = append(notManagedFeatures, feature.Name)
}
}

if feature.Spec.Disabled {
disabledFeatures = append(disabledFeatures, feature.Name)
}
}

data, err := yaml.Marshal(map[string]any{
"enabledFeatures": enabledFeatures,
"notManagedFeatures": notManagedFeatures,
"disabledFeatures": disabledFeatures,
})
if err != nil {
return ctrl.Result{}, err
}

obj := &clusterv1alpha1.ClusterClaim{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: FeatureClusterClaim,
},
}
_, err = cu.CreateOrPatch(context.TODO(), r.kc, obj, func(o client.Object, createOp bool) client.Object {

Check failure on line 90 in pkg/controllers/clusterclaim/clusterclaim.go

View workflow job for this annotation

GitHub Actions / Build

ineffectual assignment to err (ineffassign)
in := o.(*clusterv1alpha1.ClusterClaim)
in.Spec.Value = string(data)
return in
})
return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *ClusterClaimReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&uiapi.Feature{}).
Complete(r)
}
3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,9 @@ kubevault.dev/apimachinery/apis
# moul.io/http2curl/v2 v2.3.1-0.20221024080105-10c404f653f7
## explicit; go 1.13
moul.io/http2curl/v2
# open-cluster-management.io/api v0.15.0
## explicit; go 1.22.0
open-cluster-management.io/api/cluster/v1alpha1
# sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.29.0
## explicit; go 1.20
sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/client
Expand Down
201 changes: 201 additions & 0 deletions vendor/open-cluster-management.io/api/LICENSE

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

Loading

0 comments on commit 1c5da0c

Please sign in to comment.