Skip to content

Commit

Permalink
Added unit test for Keycloak
Browse files Browse the repository at this point in the history
Improve realm import namespace support
  • Loading branch information
Juansasa committed Feb 28, 2025
1 parent f641d6f commit 437fbdf
Show file tree
Hide file tree
Showing 18 changed files with 761 additions and 164 deletions.
9 changes: 9 additions & 0 deletions api/v1alpha1/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ func (s *Conditions) IsReady() bool {

return c.Status == metav1.ConditionTrue
}

func (s *Conditions) ConditionMsg(ct string) string {
c, exists := apis.GetCondition(ct, s.Conditions)
if !exists {
return ""
}

return c.Message
}
2 changes: 2 additions & 0 deletions catalog/channels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ schema: olm.channel
package: rhbk-operator
name: preview
entries:
- name: rhbk-operator.v0.0.3
replaces: rhbk-operator.v0.0.2
- name: rhbk-operator.v0.0.2
replaces: rhbk-operator.v0.0.1
- name: rhbk-operator.v0.0.1
25 changes: 24 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ go 1.22.0
toolchain go1.22.8

require (
github.com/avast/retry-go/v4 v4.6.1
github.com/go-logr/logr v1.4.2
github.com/imdario/mergo v0.3.12
github.com/onsi/ginkgo/v2 v2.20.1
github.com/onsi/gomega v1.36.1
github.com/openshift/api v0.0.0-20241007111039-82e082220d91
Expand All @@ -23,12 +25,19 @@ replace (
)

require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -41,25 +50,35 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240827171923-fa2c70bbbfe5 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
golang.org/x/text v0.19.0 // indirect
Expand All @@ -71,10 +90,14 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.31.0 // indirect
k8s.io/cli-runtime v0.28.2 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/kubectl v0.28.2 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
138 changes: 136 additions & 2 deletions go.sum

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions internal/constants/labels.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package constants

const RHBKWatchedResourceLabel = "sso.stakater.com/watched"
const RHBKRealmImportLabel = "realm.stakater.com/import"
const RHBKRealmImportRevisionLabel = "realm.stakater.com/import-rev"
const RHBKImportOwnerLabel = "realm.stakater.com/owner"
const RHBKImportNamespaceLabel = "realm.stakater.com/namepsace"
const RHBKImportRevisionLabel = "realm.stakater.com/revision"
198 changes: 116 additions & 82 deletions internal/controller/keycloak_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,132 +21,166 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
route "github.com/openshift/api/route/v1"
ssov1alpha1 "github.com/stakater/rhbk-operator/api/v1alpha1"
"github.com/stakater/rhbk-operator/test/utils/yaml"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

ssov1alpha1 "github.com/stakater/rhbk-operator/api/v1alpha1"
"github.com/stakater/rhbk-operator/internal/resources"
)

var _ = Describe("Keycloak Controller", func() {
Context("When reconciling a resource", func() {
const resourceName = "test-resource"
const resourceNs = "rhsso"
const resourceNs = "rhbk-instance"

ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: resourceNs,
}
keycloak := &ssov1alpha1.Keycloak{}
var keycloak *ssov1alpha1.Keycloak

BeforeEach(func() {
keycloak = &ssov1alpha1.Keycloak{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: resourceNs,
},
}

By("creating the custom resource for the Kind Keycloak")
err := k8sClient.Get(ctx, typeNamespacedName, keycloak)
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(keycloak), keycloak)
if err != nil && errors.IsNotFound(err) {

keycloak = &ssov1alpha1.Keycloak{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: resourceNs,
},
Spec: ssov1alpha1.KeycloakSpec{
Instances: &[]int32{1}[0],
Admin: ssov1alpha1.AdminUser{
Username: ssov1alpha1.SecretOption{
Value: "admin",
},
Password: ssov1alpha1.SecretOption{
Value: "admin",
},
},
},
}
//Expect(k8sClient.Create(ctx, keycloak)).To(Succeed())
yaml.GetResourceFromFile("keycloak.yaml", keycloak)
Expect(k8sClient.Create(ctx, keycloak)).To(Succeed())
}
})

AfterEach(func() {
keycloak = &ssov1alpha1.Keycloak{}
//err := k8sClient.Get(ctx, typeNamespacedName, keycloak)
//Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific resource instance Keycloak")
//Expect(k8sClient.Delete(ctx, keycloak)).To(Succeed())
DeleteIfExist(ctx, keycloak)
})

It("should successfully reconcile the Route resource", func() {
It("should sync statefulset", func() {
key := client.ObjectKeyFromObject(keycloak)
By("Reconciling the keycloak resource")
controllerReconciler := &KeycloakReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}
ReconcileKeycloak(ctx, key)

By("Checking Statefulset resource has been created")
statefulSet := &appsv1.StatefulSet{}
err := k8sClient.Get(ctx, key, statefulSet)

_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())
Expect(statefulSet.Name).To(Equal(resourceName))
Expect(statefulSet.Spec.Replicas).To(Equal(keycloak.Spec.Instances))
Expect(HasOwnerRef(keycloak, statefulSet)).To(BeTrue())
})

It("should setup provider download init-container", func() {
key := client.ObjectKeyFromObject(keycloak)

By("Reconciling the keycloak resource")
ReconcileKeycloak(ctx, key)

By("Checking Statefulset resource has been created")
statefulSets := &appsv1.StatefulSetList{}
_ = k8sClient.List(ctx, statefulSets, &client.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{"app": "rhbk"}),
})
//Expect(err).NotTo(HaveOccurred())
//Expect(statefulSets.Items).To(HaveLen(1))
//Expect(statefulSets.Items[0].Name).To(Equal(resourceName))
//Expect(statefulSets.Items[0].Spec.Replicas).To(Equal(keycloak.Spec.Instances))
statefulSet := &appsv1.StatefulSet{}
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(keycloak), statefulSet)

Expect(err).NotTo(HaveOccurred())
Expect(statefulSet.Spec.Template.Spec.InitContainers).To(HaveLen(1))
Expect(statefulSet.Spec.Template.Spec.InitContainers[0].Args).To(Equal([]string{
"-c",
"mkdir -p /opt/keycloak/providers; curl -LJ --show-error --capath /var/run/secrets/kubernetes.io -o /opt/keycloak/providers/keycloak-metrics-spi-6.0.0.jar $(KEYCLOAK_METRICS_SPI_6_0_0_JAR)",
}))
})

It("should successfully reconcile service resource", func() {
It("should successfully reconcile resources", func() {
key := client.ObjectKeyFromObject(keycloak)

By("Reconciling the keycloak resource")
controllerReconciler := &KeycloakReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}
ReconcileKeycloak(ctx, key)

By("Checking Statefulset resource has been created")
statefulSet := GetKeycloakStatefulSet(ctx, keycloak)
Expect(statefulSet.Name).To(Equal(resourceName))
Expect(statefulSet.Spec.Replicas).To(Equal(keycloak.Spec.Instances))
Expect(statefulSet.Spec.Template.Spec.InitContainers).To(HaveLen(1))

By("Checking route resource has been created")
route := &route.Route{}
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(keycloak), route)

_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())
Expect(route.Name).To(Equal(resourceName))

By("Checking Service resource has been created")
By("Checking svc resource has been created")
svcName := keycloak.Name + "-svc"
svc := &v1.Service{}
_ = k8sClient.Get(ctx, types.NamespacedName{
Namespace: resourceNs,
Name: resources.GetSvcName(keycloak),

_ = k8sClient.Get(ctx, client.ObjectKey{
Name: svcName,
Namespace: keycloak.Namespace,
}, svc)

Expect(err).NotTo(HaveOccurred())
Expect(svc.Name).To(Equal(svcName))

By("Checking discovery-svc resource has been created")
svcName = keycloak.Name + "-discovery"
svc = &v1.Service{}

_ = k8sClient.Get(ctx, client.ObjectKey{
Name: svcName,
Namespace: keycloak.Namespace,
}, svc)
//Expect(err).NotTo(HaveOccurred())
//Expect(svc.Name).To(Equal(resources.GetSvcName(keycloak)))

Expect(err).NotTo(HaveOccurred())
Expect(svc.Name).To(Equal(svcName))
})

It("should successfully reconcile discovery service resource", func() {
It("should reconcile changes", func() {
key := client.ObjectKeyFromObject(keycloak)

By("Reconciling the keycloak resource")
controllerReconciler := &KeycloakReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}
ReconcileKeycloak(ctx, key)

_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(keycloak), keycloak)
Expect(err).NotTo(HaveOccurred())

By("Checking Service resource has been created")
svc := &v1.Service{}
_ = k8sClient.Get(ctx, types.NamespacedName{
Namespace: resourceNs,
Name: resources.GetDiscoverySvcName(keycloak),
}, svc)
//Expect(err).NotTo(HaveOccurred())
//Expect(svc.Name).To(Equal(resources.GetDiscoverySvcName(keycloak)))
keycloak.Spec.Instances = &[]int32{1}[0]
err = k8sClient.Update(ctx, keycloak)
Expect(err).NotTo(HaveOccurred())

ReconcileKeycloak(ctx, key)

By("Checking Statefulset resource has been created")
statefulSet := &appsv1.StatefulSet{}
err = k8sClient.Get(ctx, client.ObjectKeyFromObject(keycloak), statefulSet)

Expect(err).NotTo(HaveOccurred())
Expect(statefulSet.Name).To(Equal(resourceName))
Expect(statefulSet.Spec.Replicas).To(Equal(keycloak.Spec.Instances))
})
})
})

func GetKeycloakStatefulSet(ctx context.Context, kc *ssov1alpha1.Keycloak) *appsv1.StatefulSet {
statefulSet := &appsv1.StatefulSet{}
err := k8sClient.Get(ctx, client.ObjectKeyFromObject(kc), statefulSet)
Expect(err).NotTo(HaveOccurred())
return statefulSet
}

func ReconcileKeycloak(ctx context.Context, key client.ObjectKey) {
controllerReconciler := &KeycloakReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}

_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: key,
})

Expect(err).NotTo(HaveOccurred())
}
Loading

0 comments on commit 437fbdf

Please sign in to comment.