Skip to content

Commit

Permalink
add label selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Eneman Donatien authored and Eneman Donatien committed Aug 23, 2024
1 parent 314d637 commit d24b8a2
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 25 deletions.
27 changes: 20 additions & 7 deletions controllers/bucket_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"fmt"
"time"

s3v1alpha1 "github.com/InseeFrLab/s3-operator/api/v1alpha1"
"github.com/InseeFrLab/s3-operator/controllers/s3/factory"
utils "github.com/InseeFrLab/s3-operator/controllers/utils"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -32,18 +35,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

s3v1alpha1 "github.com/InseeFrLab/s3-operator/api/v1alpha1"
"github.com/InseeFrLab/s3-operator/controllers/s3/factory"
"github.com/InseeFrLab/s3-operator/controllers/utils"
)

// BucketReconciler reconciles a Bucket object
type BucketReconciler struct {
client.Client
Scheme *runtime.Scheme
S3Client factory.S3Client
BucketDeletion bool
Scheme *runtime.Scheme
S3Client factory.S3Client
BucketDeletion bool
S3LabelSelectorValue string
}

//+kubebuilder:rbac:groups=s3.onyxia.sh,resources=buckets,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -72,6 +72,19 @@ func (r *BucketReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, err
}

// check if this object must be manage by this instance
if r.S3LabelSelectorValue != "" {
labelSelectorValue, found := bucketResource.Labels[utils.S3OperatorBucketLabelSelectorKey]
if !found {
logger.Info("This bucket ressouce will not be manage by this instance because this instance require that Bucket get labelSelector and label selector not found", "req.Name", req.Name, "Bucket Labels", bucketResource.Labels, "S3OperatorBucketLabelSelectorKey", utils.S3OperatorBucketLabelSelectorKey)
return ctrl.Result{}, nil
}
if labelSelectorValue != r.S3LabelSelectorValue {
logger.Info("This bucket ressouce will not be manage by this instance because this instance require that Bucket get specific a specific labelSelector value", "req.Name", req.Name, "expected", r.S3LabelSelectorValue, "current", labelSelectorValue)
return ctrl.Result{}, nil
}
}

// Managing bucket deletion with a finalizer
// REF : https://sdk.operatorframework.io/docs/building-operators/golang/advanced-topics/#external-resources
isMarkedForDeletion := bucketResource.GetDeletionTimestamp() != nil
Expand Down
20 changes: 17 additions & 3 deletions controllers/path_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ import (
// PathReconciler reconciles a Path object
type PathReconciler struct {
client.Client
Scheme *runtime.Scheme
S3Client factory.S3Client
PathDeletion bool
Scheme *runtime.Scheme
S3Client factory.S3Client
PathDeletion bool
S3LabelSelectorValue string
}

//+kubebuilder:rbac:groups=s3.onyxia.sh,resources=paths,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -72,6 +73,19 @@ func (r *PathReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
return ctrl.Result{}, err
}

// check if this object must be manage by this instance
if r.S3LabelSelectorValue != "" {
labelSelectorValue, found := pathResource.Labels[utils.S3OperatorPathLabelSelectorKey]
if !found {
logger.Info("This paht ressouce will not be manage by this instance because this instance require that path get labelSelector and label selector not found", "req.Name", req.Name, "Bucket Labels", pathResource.Labels, "S3OperatorBucketLabelSelectorKey", utils.S3OperatorBucketLabelSelectorKey)
return ctrl.Result{}, nil
}
if labelSelectorValue != r.S3LabelSelectorValue {
logger.Info("This path ressouce will not be manage by this instance because this instance require that path get specific a specific labelSelector value", "req.Name", req.Name, "expected", r.S3LabelSelectorValue, "current", labelSelectorValue)
return ctrl.Result{}, nil
}
}

// Managing path deletion with a finalizer
// REF : https://sdk.operatorframework.io/docs/building-operators/golang/advanced-topics/#external-resources
isMarkedForDeletion := pathResource.GetDeletionTimestamp() != nil
Expand Down
20 changes: 17 additions & 3 deletions controllers/policy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ import (
// PolicyReconciler reconciles a Policy object
type PolicyReconciler struct {
client.Client
Scheme *runtime.Scheme
S3Client factory.S3Client
PolicyDeletion bool
Scheme *runtime.Scheme
S3Client factory.S3Client
PolicyDeletion bool
S3LabelSelectorValue string
}

//+kubebuilder:rbac:groups=s3.onyxia.sh,resources=policies,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -75,6 +76,19 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, err
}

// check if this object must be manage by this instance
if r.S3LabelSelectorValue != "" {
labelSelectorValue, found := policyResource.Labels[utils.S3OperatorPolicyLabelSelectorKey]
if !found {
logger.Info("This policy ressouce will not be manage by this instance because this instance require that policy get labelSelector and label selector not found", "req.Name", req.Name, "Policy Labels", policyResource.Labels, "S3OperatorPolicyLabelSelectorKey", utils.S3OperatorPolicyLabelSelectorKey)
return ctrl.Result{}, nil
}
if labelSelectorValue != r.S3LabelSelectorValue {
logger.Info("This policy ressouce will not be manage by this instance because this instance require that policy get specific a specific labelSelector value", "req.Name", req.Name, "expected", r.S3LabelSelectorValue, "current", labelSelectorValue)
return ctrl.Result{}, nil
}
}

// Managing policy deletion with a finalizer
// REF : https://sdk.operatorframework.io/docs/building-operators/golang/advanced-topics/#external-resources
isMarkedForDeletion := policyResource.GetDeletionTimestamp() != nil
Expand Down
14 changes: 14 additions & 0 deletions controllers/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type S3UserReconciler struct {
S3Client factory.S3Client
S3UserDeletion bool
OverrideExistingSecret bool
S3LabelSelectorValue string
}

const (
Expand Down Expand Up @@ -81,6 +82,19 @@ func (r *S3UserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, err
}

// check if this object must be manage by this instance
if r.S3LabelSelectorValue != "" {
labelSelectorValue, found := userResource.Labels[utils.S3OperatorUserLabelSelectorKey]
if !found {
logger.Info("This user ressouce will not be manage by this instance because this instance require that Bucket get labelSelector and label selector not found", "req.Name", req.Name, "Bucket Labels", userResource.Labels, "S3OperatorBucketLabelSelectorKey", utils.S3OperatorBucketLabelSelectorKey)
return ctrl.Result{}, nil
}
if labelSelectorValue != r.S3LabelSelectorValue {
logger.Info("This user ressouce will not be manage by this instance because this instance require that Bucket get specific a specific labelSelector value", "req.Name", req.Name, "expected", r.S3LabelSelectorValue, "current", labelSelectorValue)
return ctrl.Result{}, nil
}
}

// Check if the userResource instance is marked to be deleted, which is
// indicated by the deletion timestamp being set. The object will be deleted.
if userResource.GetDeletionTimestamp() != nil {
Expand Down
6 changes: 6 additions & 0 deletions controllers/utils/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package utils

const S3OperatorBucketLabelSelectorKey = "s3operator.bucket.managed-by"
const S3OperatorPathLabelSelectorKey = "s3operator.path.managed-by"
const S3OperatorPolicyLabelSelectorKey = "s3operator.policy.managed-by"
const S3OperatorUserLabelSelectorKey = "s3operator.user.managed-by"
30 changes: 18 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func main() {
var policyDeletion bool
var pathDeletion bool
var s3userDeletion bool
var s3LabelSelector string

//K8S related variable
var overrideExistingSecret bool
Expand All @@ -97,6 +98,7 @@ func main() {
flag.StringVar(&s3EndpointUrl, "s3-endpoint-url", "localhost:9000", "Hostname (or hostname:port) of the S3 server")
flag.StringVar(&accessKey, "s3-access-key", "ROOTNAME", "The accessKey of the acount")
flag.StringVar(&secretKey, "s3-secret-key", "CHANGEME123", "The secretKey of the acount")
flag.StringVar(&s3LabelSelector, "s3-label-selector", "", "label selector to filter object managed by this operator if empty all objects are managed")
flag.Var(&caCertificatesBase64, "s3-ca-certificate-base64", "(Optional) Base64 encoded, PEM format certificate file for a certificate authority, for https requests to S3")
flag.StringVar(&caCertificatesBundlePath, "s3-ca-certificate-bundle-path", "", "(Optional) Path to a CA certificate file, for https requests to S3")
flag.StringVar(&region, "region", "us-east-1", "The region to configure for the S3 client")
Expand Down Expand Up @@ -173,28 +175,31 @@ func main() {
}

if err = (&controllers.BucketReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
S3Client: s3Client,
BucketDeletion: bucketDeletion,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
S3Client: s3Client,
BucketDeletion: bucketDeletion,
S3LabelSelectorValue: s3LabelSelector,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Bucket")
os.Exit(1)
}
if err = (&controllers.PathReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
S3Client: s3Client,
PathDeletion: pathDeletion,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
S3Client: s3Client,
PathDeletion: pathDeletion,
S3LabelSelectorValue: s3LabelSelector,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Path")
os.Exit(1)
}
if err = (&controllers.PolicyReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
S3Client: s3Client,
PolicyDeletion: policyDeletion,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
S3Client: s3Client,
PolicyDeletion: policyDeletion,
S3LabelSelectorValue: s3LabelSelector,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Policy")
os.Exit(1)
Expand All @@ -205,6 +210,7 @@ func main() {
S3Client: s3Client,
S3UserDeletion: s3userDeletion,
OverrideExistingSecret: overrideExistingSecret,
S3LabelSelectorValue: s3LabelSelector,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "S3User")
os.Exit(1)
Expand Down

0 comments on commit d24b8a2

Please sign in to comment.