Skip to content

Commit

Permalink
Merge "reconcile - Remove Owns(Secrets) style watcher and instead wat…
Browse files Browse the repository at this point in the history
…ch specific ones"
  • Loading branch information
Microzuul CI authored and Gerrit Code Review committed Nov 14, 2023
2 parents c28dee0 + d790154 commit 66b71d8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
16 changes: 16 additions & 0 deletions api/v1/softwarefactory_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ func GetGitHubConnectionsName(spec *ZuulSpec) []string {
return res
}

func GetGitHubConnectionsSecretName(spec *ZuulSpec) []string {
var res []string
for _, conn := range spec.GitHubConns {
res = append(res, conn.Secrets)
}
return res
}

func GetGitLabConnectionsName(spec *ZuulSpec) []string {
var res []string
for _, conn := range spec.GitLabConns {
Expand All @@ -289,6 +297,14 @@ func GetGitLabConnectionsName(spec *ZuulSpec) []string {
return res
}

func GetGitLabConnectionsSecretName(spec *ZuulSpec) []string {
var res []string
for _, conn := range spec.GitLabConns {
res = append(res, conn.Secrets)
}
return res
}

// +kubebuilder:validation:Enum=INFO;WARN;DEBUG
// +kubebuilder:default:=INFO
type LogLevel string
Expand Down
1 change: 0 additions & 1 deletion controllers/logserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ func (r *LogServerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
func (r *LogServerReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&sfv1.LogServer{}).
Owns(&apiv1.Secret{}).
Owns(&certv1.Certificate{}).
Complete(r)
}
46 changes: 45 additions & 1 deletion controllers/softwarefactory_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ import (
"github.com/fatih/color"

"k8s.io/client-go/rest"
"k8s.io/utils/strings/slices"

corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

sfv1 "github.com/softwarefactory-project/sf-operator/api/v1"
"github.com/softwarefactory-project/sf-operator/controllers/libs/conds"
Expand Down Expand Up @@ -282,7 +286,47 @@ func (r *SoftwareFactoryReconciler) StandaloneReconcile(ctx context.Context, ns
func (r *SoftwareFactoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&sfv1.SoftwareFactory{}).
Owns(&corev1.Secret{}).
// Watch only specific Secrets resources
Watches(
&corev1.Secret{},
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []reconcile.Request {
softwareFactories := sfv1.SoftwareFactoryList{}
r.Client.List(ctx, &softwareFactories, &client.ListOptions{
Namespace: a.GetNamespace(),
})
if len(softwareFactories.Items) > 0 {
// We take the first one of the list
// sf-operator only manages one SoftwareFactory instance by namespace
softwareFactory := softwareFactories.Items[0]
req := []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: softwareFactory.Name,
Namespace: a.GetNamespace(),
}}}
switch updatedResourceName := a.GetName(); updatedResourceName {
case NodepoolProvidersSecretsName:
return req
case GetCustomRouteSSLSecretName("logserver"):
return req
case GetCustomRouteSSLSecretName("nodepool"):
return req
case GetCustomRouteSSLSecretName("zuul"):
return req
default:
// Discover secrets for github and gitlab connections
otherSecretNames := []string{}
otherSecretNames = append(otherSecretNames, sfv1.GetGitHubConnectionsSecretName(&softwareFactory.Spec.Zuul)...)
otherSecretNames = append(otherSecretNames, sfv1.GetGitLabConnectionsSecretName(&softwareFactory.Spec.Zuul)...)
if slices.Contains(otherSecretNames, a.GetName()) {
return req
}
// All others secrets must trigger reconcile
return []reconcile.Request{}
}
}
return []reconcile.Request{}
}),
).
Owns(&certv1.Certificate{}).
Complete(r)
}

0 comments on commit 66b71d8

Please sign in to comment.