Skip to content

Commit

Permalink
Redesign the operator to use a single namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoussaud committed Jan 15, 2024
1 parent c1adb46 commit 35a4de8
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 166 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.moussaud.tanzu.tapoperator.controller.tanzusync;
package org.moussaud.tanzu.tapoperator.controller;

import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.moussaud.tanzu.tapoperator.controller.tanzusync;
package org.moussaud.tanzu.tapoperator.controller;

import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.moussaud.tanzu.tapoperator.controller.tanzusync;
package org.moussaud.tanzu.tapoperator.controller;

import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.k14s.kappctrl.v1alpha1.App;
Expand All @@ -21,10 +21,10 @@
import java.util.Collections;
import java.util.List;

public class AppResource extends TanzuSyncResource<App> {
public class AppResource extends BaseResource<App> {
public static final String COMPONENT = "sync";

private static final Logger log = LoggerFactory.getLogger(TanzuSyncResource.class);
private static final Logger log = LoggerFactory.getLogger(AppResource.class);

public AppResource() {
super(App.class, COMPONENT);
Expand All @@ -39,20 +39,20 @@ protected App desired(TapResource primary, Context<TapResource> context) {
desired.setMetadata(createMeta(primary).build());
AppSpec spec = new AppSpec();
desired.setSpec(spec);
spec.setServiceAccountName(ServiceAccountResource.COMPONENT);
spec.setServiceAccountName(new ServiceAccountResource().name(primary));
Git git = new Git();
git.setRef("origin/main");
git.setUrl(primary.getSpec().getUrl());
git.setSubPath(primary.getSpec().getSubPath());
var secret = new SecretRef();
secret.setName(SecretSyncGitResource.COMPONENT);
secret.setName(new SecretSyncGitResource().name(primary));
git.setSecretRef(secret);
Fetch fetch = new Fetch();
fetch.setGit(git);
spec.setFetch(Collections.singletonList(fetch));

var pkr = new PrivateKeysSecretRef();
pkr.setName(SecretSyncAgeIdentityResource.COMPONENT);
pkr.setName(new SecretSyncAgeIdentityResource().name(primary));
var age = new Age();
age.setPrivateKeysSecretRef(pkr);
var sops = new Sops();
Expand All @@ -77,4 +77,22 @@ protected App desired(TapResource primary, Context<TapResource> context) {

return desired;
}

public String name(TapResource primary) {
return getComponent();
}

@Override
protected void handleDelete(TapResource primary, App secondary, Context<TapResource> context) {
log.info("handleDelete {} {}", name(primary), resourceType());
super.handleDelete(primary, secondary, context);
}

@Override
public boolean isDeletable() {
log.trace("isDeletable {} but True...to trigger the deletion of the App", super.isDeletable());
return true;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
import org.jetbrains.annotations.NotNull;
import org.moussaud.tanzu.tapoperator.controller.tanzusync.TanzuSyncResource;
import org.moussaud.tanzu.tapoperator.resource.TapResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -31,16 +30,18 @@ public BaseResource(Class<R> resourceType, String component) {
setResourceDiscriminator(new Discriminator(component));
}

@Override
public String getComponent() {
return component;
}

class Discriminator
extends ResourceIDMatcherDiscriminator<R, TapResource> {
public Discriminator(String component) {
super(p -> new ResourceID(name(p), namespace(p)));
}
}

public String name(TapResource primary) {
return "%s-%s".formatted(primary.getMetadata().getName(), component);
}

protected ObjectMetaBuilder createMeta(TapResource primary) {
return new ObjectMetaBuilder()
Expand All @@ -51,7 +52,7 @@ protected ObjectMetaBuilder createMeta(TapResource primary) {
.addToLabels(K8S_MANAGED_BY, K8S_OWNER);
}

protected String namespace(TapResource primary) {
public String namespace(TapResource primary) {
return primary.getMetadata().getNamespace();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.moussaud.tanzu.tapoperator.controller;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.javaoperatorsdk.operator.api.reconciler.dependent.Deleter;
import io.javaoperatorsdk.operator.processing.dependent.Creator;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource;
import org.moussaud.tanzu.tapoperator.resource.TapResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClusterBaseResource<R extends HasMetadata> extends KubernetesDependentResource<R, TapResource>
implements Creator<R, TapResource>, Deleter<TapResource>, TapOperatorManagedResource {

private static final Logger log = LoggerFactory.getLogger(ClusterBaseResource.class);
private final String component;

public ClusterBaseResource(Class<R> resourceType, String component) {
super(resourceType);
this.component = component;
}

@Override
public String name(TapResource primary) {
return component;
}


@Override
public String getComponent() {
return component;
}

private static final String K8S_NAME = "app.kubernetes.io/name";
private static final String K8S_COMPONENT = "app.kubernetes.io/component";
private static final String K8S_MANAGED_BY = "app.kubernetes.io/managed-by";
private static final String K8S_OWNER = "tap-operator";

protected ObjectMetaBuilder createMeta(TapResource primary) {
return new ObjectMetaBuilder()
.withName(name(primary))
.addToLabels(K8S_NAME, component)
.addToLabels(K8S_COMPONENT, component)
.addToLabels(K8S_MANAGED_BY, K8S_OWNER);
}


}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.moussaud.tanzu.tapoperator.controller.tanzusync;

import org.moussaud.tanzu.tapoperator.resource.TapResource;
package org.moussaud.tanzu.tapoperator.controller;

import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding;
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder;
import io.fabric8.kubernetes.api.model.rbac.SubjectBuilder;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.fabric8.kubernetes.api.model.rbac.*;
import org.moussaud.tanzu.tapoperator.resource.TapResource;

public class ClusterRoleBindingResource extends TanzuSyncResource<ClusterRoleBinding> {
public class ClusterRoleBindingResourceCluster extends ClusterBaseResource<ClusterRoleBinding> {
public static final String COMPONENT = "tanzu-sync-cluster-crb-admin";

public ClusterRoleBindingResource() {
public ClusterRoleBindingResourceCluster() {
super(ClusterRoleBinding.class, COMPONENT);
}

Expand All @@ -18,11 +19,11 @@ protected ClusterRoleBinding desired(TapResource primary, Context<TapResource> c
return new ClusterRoleBindingBuilder()
.withMetadata(createMeta(primary).build())
.withNewRoleRef("rbac.authorization.k8s.io", "ClusterRole",
ClusterRoleResource.COMPONENT)
ClusterRoleResourceCluster.COMPONENT)
.withSubjects(
new SubjectBuilder()
.withName(ServiceAccountResource.COMPONENT)
.withNamespace(NamespaceResource.COMPONENT)
.withName(new ServiceAccountResource().name(primary))
.withNamespace(new ServiceAccountResource().namespace(primary))
.withKind("ServiceAccount")
.build())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.moussaud.tanzu.tapoperator.controller.tanzusync;

import org.moussaud.tanzu.tapoperator.resource.TapResource;
package org.moussaud.tanzu.tapoperator.controller;

import io.fabric8.kubernetes.api.model.rbac.ClusterRole;
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBuilder;
import io.fabric8.kubernetes.api.model.rbac.PolicyRuleBuilder;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import org.moussaud.tanzu.tapoperator.resource.TapResource;

public class ClusterRoleResource extends TanzuSyncResource<ClusterRole> {
public class ClusterRoleResourceCluster extends ClusterBaseResource<ClusterRole> {
public static final String COMPONENT = "tanzu-sync-cluster-admin";

public ClusterRoleResource() {
public ClusterRoleResourceCluster() {
super(ClusterRole.class, COMPONENT);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.moussaud.tanzu.tapoperator.controller.tanzusync;
package org.moussaud.tanzu.tapoperator.controller;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.moussaud.tanzu.tapoperator.controller.tanzusync;
package org.moussaud.tanzu.tapoperator.controller;

import io.javaoperatorsdk.operator.api.reconciler.Context;
import dev.carvel.secretgen.v1alpha1.*;

import java.util.Collections;

import org.moussaud.tanzu.tapoperator.controller.BaseResource;
import org.moussaud.tanzu.tapoperator.resource.*;

public class SecretExportResource extends TanzuSyncResource<SecretExport> {
public class SecretExportResource extends BaseResource<SecretExport> {
public static final String COMPONENT = "install-registry-dockerconfig";

public SecretExportResource() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.moussaud.tanzu.tapoperator.controller.tanzusync;
package org.moussaud.tanzu.tapoperator.controller;

import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretBuilder;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import org.moussaud.tanzu.tapoperator.controller.Utils;
import org.moussaud.tanzu.tapoperator.resource.TapResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SecretInstallRegistryDockerConfigResource extends TanzuSyncResource<Secret> {
public class SecretInstallRegistryDockerConfigResource extends BaseResource<Secret> {

private static final Logger log = LoggerFactory.getLogger(SecretInstallRegistryDockerConfigResource.class);

public static final String COMPONENT = "install-registry-dockerconfig";

Expand All @@ -16,6 +19,7 @@ public SecretInstallRegistryDockerConfigResource() {

@Override
protected Secret desired(TapResource primary, Context<TapResource> context) {
log.info("Desired {} {}", name(primary), resourceType());
var secret = getSecret(primary, context);
return new SecretBuilder()
.withMetadata(createMeta(primary).build())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.moussaud.tanzu.tapoperator.controller.tanzusync;
package org.moussaud.tanzu.tapoperator.controller;

import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretBuilder;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import org.moussaud.tanzu.tapoperator.controller.Utils;
import org.moussaud.tanzu.tapoperator.resource.TapResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SecretSyncAgeIdentityResource extends TanzuSyncResource<Secret> {
public class SecretSyncAgeIdentityResource extends BaseResource<Secret> {

private static final Logger log = LoggerFactory.getLogger(SecretSyncAgeIdentityResource.class);

public static final String COMPONENT = "sync-age-identity";

Expand All @@ -16,6 +19,7 @@ public SecretSyncAgeIdentityResource() {

@Override
protected Secret desired(TapResource primary, Context<TapResource> context) {
log.info("Desired {} {}", name(primary), resourceType());
var secret = getSecret(primary, context);
return new SecretBuilder()
.withMetadata(createMeta(primary).build())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.moussaud.tanzu.tapoperator.controller.tanzusync;
package org.moussaud.tanzu.tapoperator.controller;

import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretBuilder;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import org.moussaud.tanzu.tapoperator.controller.Utils;
import org.moussaud.tanzu.tapoperator.resource.TapResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SecretSyncGitResource extends TanzuSyncResource<Secret> {
public class SecretSyncGitResource extends BaseResource<Secret> {

public static final String COMPONENT = "sync-git";

Expand All @@ -20,6 +19,7 @@ public SecretSyncGitResource() {

@Override
protected Secret desired(TapResource primary, Context<TapResource> context) {
log.info("Desired {} {}", name(primary), resourceType());
var secret = getSecret(primary, context);
var result = Utils.getSyncGit(secret.getData());
if (result.containsKey("ssh-privatekey") && !result.containsKey("ssh-knownhosts")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.moussaud.tanzu.tapoperator.controller.tanzusync;
package org.moussaud.tanzu.tapoperator.controller;

import io.fabric8.kubernetes.api.model.ServiceAccount;
import io.fabric8.kubernetes.api.model.ServiceAccountBuilder;
Expand All @@ -7,7 +7,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ServiceAccountResource extends TanzuSyncResource<ServiceAccount> {
public class ServiceAccountResource extends BaseResource<ServiceAccount> {
public static final String COMPONENT = "sync-sa";

private static final Logger log = LoggerFactory.getLogger(ServiceAccountResource.class);
Expand All @@ -18,8 +18,10 @@ public ServiceAccountResource() {

@Override
protected ServiceAccount desired(TapResource primary, Context<TapResource> context) {
log.info("Desired {} {}", name(primary), resourceType());
return new ServiceAccountBuilder()
.withMetadata(createMeta(primary).build())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
import org.moussaud.tanzu.tapoperator.resource.TapResource;

public interface TapOperatorManagedResource {
public String name(TapResource resource);

public String getComponent();

default public String name(TapResource primary) {
return "%s-%s".formatted(primary.getMetadata().getName(), getComponent());
}
}
Loading

0 comments on commit 35a4de8

Please sign in to comment.