Skip to content

update controller runtime #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ manager: generate fmt vet
-X 'github.com/metal-stack/v.GitSHA1=$(SHA)' \
-X 'github.com/metal-stack/v.BuildDate=$(BUILDDATE)'" \
-o bin/manager main.go
strip bin/manager

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt vet manifests install-crd-cwnp
Expand Down
44 changes: 21 additions & 23 deletions controllers/postgres_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ type PostgresReconciler struct {
// +kubebuilder:rbac:groups=database.fits.cloud,resources=postgres/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=acid.zalan.do,resources=postgresqls,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=acid.zalan.do,resources=postgresqls/status,verbs=get;list;watch
func (r *PostgresReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("postgres", req.NamespacedName)

log.Info("fetchting postgres")
Expand Down Expand Up @@ -92,14 +91,13 @@ func (r *PostgresReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {

deletable, err := r.IsOperatorDeletable(ctx, namespace)
if err != nil {
return ctrl.Result{}, fmt.Errorf("error while checking if the operator is idle: %v", err)
return ctrl.Result{}, fmt.Errorf("error while checking if the operator is idle: %w", err)
}
if !deletable {
log.Info("operator not deletable")
return ctrl.Result{Requeue: true}, nil
}
if err := r.UninstallOperator(ctx, namespace); err != nil {
return ctrl.Result{}, fmt.Errorf("error while uninstalling operator: %v", err)
if deletable {
log.Info("operator deletable")
if err := r.UninstallOperator(ctx, namespace); err != nil {
return ctrl.Result{}, fmt.Errorf("error while uninstalling operator: %w", err)
}
}

instance.RemoveFinalizer(pg.PostgresFinalizerName)
Expand All @@ -114,15 +112,15 @@ func (r *PostgresReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
log.Info("finalizer being added")
instance.AddFinalizer(pg.PostgresFinalizerName)
if err := r.Update(ctx, instance); err != nil {
return requeue, fmt.Errorf("error while adding finalizer: %v", err)
return requeue, fmt.Errorf("error while adding finalizer: %w", err)
}
log.Info("finalizer added")
return ctrl.Result{}, nil
}

// Check if zalando dependencies are installed. If not, install them.
if err := r.ensureZalandoDependencies(ctx, instance); err != nil {
return ctrl.Result{}, fmt.Errorf("error while ensuring Zalando dependencies: %v", err)
return ctrl.Result{}, fmt.Errorf("error while ensuring Zalando dependencies: %w", err)
}

if err := r.createOrUpdateZalandoPostgresql(ctx, instance, log); err != nil {
Expand All @@ -142,7 +140,7 @@ func (r *PostgresReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {

// Update status will be handled by the StatusReconciler, based on the Zalando Status
if err := r.createOrUpdateCWNP(ctx, instance, int(port)); err != nil {
return ctrl.Result{}, fmt.Errorf("unable to create or update corresponding CRD ClusterwideNetworkPolicy: %W", err)
return ctrl.Result{}, fmt.Errorf("unable to create or update corresponding CRD ClusterwideNetworkPolicy: %w", err)
}

return ctrl.Result{}, nil
Expand Down Expand Up @@ -217,7 +215,7 @@ func (r *PostgresReconciler) ensureZalandoDependencies(ctx context.Context, p *p
namespace := p.ToPeripheralResourceNamespace()
isInstalled, err := r.IsOperatorInstalled(ctx, namespace)
if err != nil {
return fmt.Errorf("error while querying if zalando dependencies are installed: %v", err)
return fmt.Errorf("error while querying if zalando dependencies are installed: %w", err)
}

// fetch secret
Expand All @@ -227,12 +225,12 @@ func (r *PostgresReconciler) ensureZalandoDependencies(ctx context.Context, p *p
Namespace: p.Namespace,
}
if err := r.Client.Get(ctx, backupNamespace, backupSecret); err != nil {
return fmt.Errorf("error while getting the backup secret from control plane cluster: %v", err)
return fmt.Errorf("error while getting the backup secret from control plane cluster: %w", err)
}

s3url, err := url.Parse(string(backupSecret.Data[pg.BackupSecretS3Endpoint]))
if err != nil {
return fmt.Errorf("error while parsing the s3 endpoint url in the backup secret: %v", err)
return fmt.Errorf("error while parsing the s3 endpoint url in the backup secret: %w", err)
}
// use the s3 endpoint as provided
awsEndpoint := s3url.String()
Expand All @@ -251,7 +249,7 @@ func (r *PostgresReconciler) ensureZalandoDependencies(ctx context.Context, p *p
if !isInstalled {
_, err := r.InstallOperator(ctx, namespace, awsEndpoint+"/"+bucketName) // TODO check the s3BucketUrl...
if err != nil {
return fmt.Errorf("error while installing zalando dependencies: %v", err)
return fmt.Errorf("error while installing zalando dependencies: %w", err)
}
}

Expand Down Expand Up @@ -281,11 +279,11 @@ func (r *PostgresReconciler) ensureZalandoDependencies(ctx context.Context, p *p
Namespace: p.ToPeripheralResourceNamespace(),
}
if err := r.Service.Get(ctx, ns, cm); err != nil {
return fmt.Errorf("error while getting the pod environment configmap from service cluster: %v", err)
return fmt.Errorf("error while getting the pod environment configmap from service cluster: %w", err)
}
cm.Data = data
if err := r.Service.Update(ctx, cm); err != nil {
return fmt.Errorf("error while updating the pod environment configmap in service cluster: %v", err)
return fmt.Errorf("error while updating the pod environment configmap in service cluster: %w", err)
}

return nil
Expand All @@ -311,10 +309,10 @@ func (r *PostgresReconciler) deleteZPostgresqlByLabels(ctx context.Context, matc
return err
}

for _, rawZ := range items {
log := r.Log.WithValues("zalando postgrsql", rawZ)
if err := r.Service.Delete(ctx, &rawZ); err != nil {
return fmt.Errorf("error while deleting zalando postgresql: %v", err)
for i, rawZ := range items {
log := r.Log.WithValues("zalando postgresql", rawZ)
if err := r.Service.Delete(ctx, &items[i]); err != nil {
return fmt.Errorf("error while deleting zalando postgresql: %w", err)
}
log.Info("zalando postgresql deleted")
}
Expand All @@ -336,7 +334,7 @@ func (r *PostgresReconciler) createOrUpdateCWNP(ctx context.Context, in *pg.Post
if _, err := controllerutil.CreateOrUpdate(ctx, r.Service, key, func() error {
key.Spec.Ingress = policy.Spec.Ingress
return nil
}); err != nil {
}); err != nil && !errors.IsAlreadyExists(err) {
return fmt.Errorf("unable to deploy CRD ClusterwideNetworkPolicy: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions controllers/status_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ type StatusReconciler struct {
// Reconcile updates the status of the remote Postgres object based on the status of the local zalando object.
// +kubebuilder:rbac:groups=acid.zalan.do,resources=postgresqls,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=acid.zalan.do,resources=postgresqls/status,verbs=get;update;patch
func (r *StatusReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
func (r *StatusReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("postgresql", req.NamespacedName)

log.Info("fetching postgresql")
Expand Down
4 changes: 0 additions & 4 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

databasev1 "github.com/fi-ts/postgreslet/api/v1"
zalando "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
Expand All @@ -41,8 +39,6 @@ func TestAPIs(t *testing.T) {
}

var _ = BeforeSuite(func(done Done) {
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
k8s.io/apiextensions-apiserver v0.20.4
k8s.io/apimachinery v0.20.4
k8s.io/client-go v11.0.0+incompatible
sigs.k8s.io/controller-runtime v0.6.4
sigs.k8s.io/controller-runtime v0.8.2
)

replace k8s.io/client-go => k8s.io/client-go v0.20.4
Loading