Skip to content

Commit

Permalink
Add disabling instance feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Saratura committed Mar 19, 2024
1 parent 9929738 commit 8139e31
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 30 deletions.
2 changes: 2 additions & 0 deletions api/v1alpha1/stardoginstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type StardogInstanceSpec struct {
// This is used by the Operator to make changes in the roles, permissions and users.
// +kubebuilder:validation:Required
AdminCredentials StardogUserCredentialsSpec `json:"adminCredentials,omitempty"`
// Disabled whether this instance is disabled or enabled for operator to recycle resources
Disabled bool `json:"disabled,omitempty"`
}

// StardogInstanceStatus defines the observed state of StardogInstance
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/stardog.vshn.ch_stardoginstances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ spec:
the "username" and "password" keys.
type: string
type: object
disabled:
description: Disabled whether this instance is disabled or enabled
for operator to recycle resources
type: boolean
serverUrl:
description: ServerUrl describes the url of the Stardog Instance
type: string
Expand Down
20 changes: 14 additions & 6 deletions controllers/database_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,13 @@ func (r *DatabaseReconciler) deleteDatabase(dr *DatabaseReconciliation, instance
database := dr.resource

r.Log.V(1).Info("setup Stardog Client from ", "ref", instance)
auth, err := dr.reconciliationContext.initStardogClientFromRef(r.Client, instance)
if err != nil {
return err
auth, disabled, err := dr.reconciliationContext.initStardogClientFromRef(r.Client, instance)
if err != nil || disabled {
if err != nil {
return fmt.Errorf("cannot initialize stardog client: %v", err)
}
r.Log.Info("skipping resource from reconciliation", "instance", instance.Name, "resource", dr.resource.Name)
return nil
}

stardogClient := dr.reconciliationContext.stardogClient
Expand Down Expand Up @@ -333,9 +337,13 @@ func (r *DatabaseReconciler) sync(dr *DatabaseReconciliation, instance stardogv1
customUser := database.Spec.AddUserForNonHiddenGraphs
customUserEnabled := customUser != ""

auth, err := rc.initStardogClientFromRef(r.Client, instance)
if err != nil {
return fmt.Errorf("cannot initialize stardog client: %v", err)
auth, disabled, err := rc.initStardogClientFromRef(r.Client, instance)
if err != nil || disabled {
if err != nil {
return fmt.Errorf("cannot initialize stardog client: %v", err)
}
r.Log.Info("skipping resource from reconciliation", "instance", instance.Name, "resource", dr.resource.Name)
return nil
}

// Generate and save credentials in k8s
Expand Down
20 changes: 14 additions & 6 deletions controllers/organization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,13 @@ func (r *OrganizationReconciler) sync(or *OrganizationReconciliation, instance s
orgName := org.Spec.Name
stardogClient := or.reconciliationContext.stardogClient

auth, err := rc.initStardogClientFromRef(r.Client, instance)
if err != nil {
return fmt.Errorf("cannot initialize stardog client: %v", err)
auth, disabled, err := rc.initStardogClientFromRef(r.Client, instance)
if err != nil || disabled {
if err != nil {
return fmt.Errorf("cannot initialize stardog client: %v", err)
}
r.Log.Info("skipping resource from reconciliation", "instance", instance.Name, "resource", or.resource.Name)
return nil
}

// Generate and save credentials in k8s
Expand Down Expand Up @@ -418,9 +422,13 @@ func (r *OrganizationReconciler) deleteOrganization(or *OrganizationReconciliati
orgName := org.Spec.Name

r.Log.V(1).Info("setup Stardog Client from ", "ref", instance)
auth, err := or.reconciliationContext.initStardogClientFromRef(r.Client, instance)
if err != nil {
return err
auth, disabled, err := or.reconciliationContext.initStardogClientFromRef(r.Client, instance)
if err != nil || disabled {
if err != nil {
return fmt.Errorf("cannot initialize stardog client: %v", err)
}
r.Log.Info("skipping resource from reconciliation", "instance", instance.Name, "resource", or.resource.Name)
return nil
}

stardogClient := or.reconciliationContext.stardogClient
Expand Down
14 changes: 11 additions & 3 deletions controllers/reconciliation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,22 @@ func (rc *ReconciliationContext) initStardogClient(kubeClient client.Client, sta
return auth.BasicAuth(adminUsername, adminPassword), nil
}

func (rc *ReconciliationContext) initStardogClientFromRef(kubeClient client.Client, instance v1beta1.StardogInstanceRef) (runtime.ClientAuthInfoWriter, error) {
func (rc *ReconciliationContext) initStardogClientFromRef(kubeClient client.Client, instance v1beta1.StardogInstanceRef) (runtime.ClientAuthInfoWriter, bool, error) {
stardogInstance := &StardogInstance{}
err := kubeClient.Get(rc.context, types.NamespacedName{Namespace: instance.Namespace, Name: instance.Name}, stardogInstance)
if err != nil {
return nil, fmt.Errorf("cannot retrieve stardogInstanceRef %s/%s: %v", instance.Namespace, instance.Name, err)
return nil, true, fmt.Errorf("cannot retrieve stardogInstanceRef %s/%s: %v", instance.Namespace, instance.Name, err)
}
if stardogInstance.Spec.Disabled {
return nil, true, nil
}
rc.namespace = stardogInstance.Namespace
return rc.initStardogClient(kubeClient, *stardogInstance)
stardogClient, err := rc.initStardogClient(kubeClient, *stardogInstance)
if err != nil {
return nil, true, err
}

return stardogClient, false, nil
}

func (rc *ReconciliationContext) getCredentials(kubeClient client.Client, credentials StardogUserCredentialsSpec, alternativeNamespace string) (username, password string, err error) {
Expand Down
2 changes: 1 addition & 1 deletion controllers/reconciliation_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Test_initStardogClientFromRef(t *testing.T) {

base64.StdEncoding.EncodeToString([]byte(username))

_, err = rc.initStardogClientFromRef(fakeKubeClient, stardogInstanceRef)
_, _, err = rc.initStardogClientFromRef(fakeKubeClient, stardogInstanceRef)

assert.Equal(t, tt.err, err)
})
Expand Down
4 changes: 4 additions & 0 deletions controllers/stardoginstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ func (r *StardogInstanceReconciler) validateConnection(sir *StardogInstanceRecon
spec := sir.resource.Spec
credentials := spec.AdminCredentials

if spec.Disabled {
return nil
}

r.Log.V(1).Info("retrieving admin credentials from Secret", "secret", credentials.Namespace+"/"+credentials.SecretRef)
auth, err := rc.initStardogClient(r.Client, *sir.resource)
if err != nil {
Expand Down
24 changes: 16 additions & 8 deletions controllers/stardogrole_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,20 @@ func (r *StardogRoleReconciler) syncRole(srr *StardogRoleReconciliation) error {
spec := srr.resource.Spec
namespace := srr.reconciliationContext.namespace
roleName := spec.RoleName
instance := v1beta1.NewStardogInstanceRef(spec.StardogInstanceRef, namespace)
if roleName == "" {
roleName = srr.resource.Name
}

r.Log.V(1).Info("init Stardog Client from ", "ref", spec.StardogInstanceRef)
auth, err := srr.reconciliationContext.initStardogClientFromRef(r.Client, v1beta1.NewStardogInstanceRef(spec.StardogInstanceRef, namespace))
if err != nil {
return err
auth, disabled, err := srr.reconciliationContext.initStardogClientFromRef(r.Client, instance)
if err != nil || disabled {
if err != nil {
return fmt.Errorf("cannot initialize stardog client: %v", err)
}
r.Log.Info("skipping resource from reconciliation", "instance", instance.Name, "resource", srr.resource.Name)
return nil
}

stardogClient := srr.reconciliationContext.stardogClient

r.Log.Info("synchronizing role", "role", roleName)
Expand Down Expand Up @@ -251,11 +255,15 @@ func (r *StardogRoleReconciler) deleteStardogRole(srr *StardogRoleReconciliation
func (r *StardogRoleReconciler) finalize(srr *StardogRoleReconciliation) error {
spec := srr.resource.Spec
namespace := srr.reconciliationContext.namespace

instance := v1beta1.NewStardogInstanceRef(spec.StardogInstanceRef, namespace)
r.Log.V(1).Info("setup Stardog Client from ", "ref", spec.StardogInstanceRef)
auth, err := srr.reconciliationContext.initStardogClientFromRef(r.Client, v1beta1.NewStardogInstanceRef(spec.StardogInstanceRef, namespace))
if err != nil {
return err
auth, disabled, err := srr.reconciliationContext.initStardogClientFromRef(r.Client, instance)
if err != nil || disabled {
if err != nil {
return fmt.Errorf("cannot initialize stardog client: %v", err)
}
r.Log.Info("skipping resource from reconciliation", "instance", instance.Name, "resource", srr.resource.Name)
return nil
}

stardogClient := srr.reconciliationContext.stardogClient
Expand Down
22 changes: 16 additions & 6 deletions controllers/stardoguser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,16 @@ func (r *StardogUserReconciler) finalize(sur *StardogUserReconciliation) error {
rc := sur.reconciliationContext
spec := sur.resource.Spec
namespace := rc.namespace
instance := v1beta1.NewStardogInstanceRef(spec.StardogInstanceRef, namespace)

r.Log.V(1).Info("setup Stardog Client from ", "ref", spec.StardogInstanceRef)
auth, err := rc.initStardogClientFromRef(r.Client, v1beta1.NewStardogInstanceRef(spec.StardogInstanceRef, namespace))
if err != nil {
return err
auth, disabled, err := rc.initStardogClientFromRef(r.Client, instance)
if err != nil || disabled {
if err != nil {
return fmt.Errorf("cannot initialize stardog client: %v", err)
}
r.Log.Info("skipping resource from reconciliation", "instance", instance.Name, "resource", sur.resource.Name)
return nil
}

_, err = rc.stardogClient.Users.RemoveUser(model_users.NewRemoveUserParams().WithUser(sur.resource.Name), auth)
Expand All @@ -167,11 +172,16 @@ func (r *StardogUserReconciler) syncUser(sur *StardogUserReconciliation) error {
spec := sur.resource.Spec
userCredentials := spec.Credentials
namespace := rc.namespace
instance := v1beta1.NewStardogInstanceRef(spec.StardogInstanceRef, namespace)

r.Log.V(1).Info("init Stardog Client from ", "ref", spec.StardogInstanceRef)
auth, err := rc.initStardogClientFromRef(r.Client, v1beta1.NewStardogInstanceRef(spec.StardogInstanceRef, namespace))
if err != nil {
return err
auth, disabled, err := rc.initStardogClientFromRef(r.Client, instance)
if err != nil || disabled {
if err != nil {
return fmt.Errorf("cannot initialize stardog client: %v", err)
}
r.Log.Info("skipping resource from reconciliation", "instance", instance.Name, "resource", sur.resource.Name)
return nil
}

r.Log.V(1).Info("retrieving user credentials from Secret", "secret", userCredentials.Namespace+"/"+userCredentials.SecretRef)
Expand Down

0 comments on commit 8139e31

Please sign in to comment.