Skip to content
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

feat: use label selector to pick namespace #1427

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
114 changes: 103 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,12 @@ func main() { //nolint:funlen,maintidx
os.Exit(1)
}

secretCache := createSecretCacheConfig(platform)
deploymentCache := createDeploymentCacheConfig(platform)
// get old release version before we create default DSCI CR
oldReleaseVersion, _ := upgrade.GetDeployedRelease(ctx, setupClient)

secretCache := createSecretCacheConfig(ctx, setupClient, !(len(oldReleaseVersion.Name) == 0), platform)
oDHCache := createODHGeneralCacheConfig(ctx, setupClient, !(len(oldReleaseVersion.Name) == 0), platform)

cacheOptions := cache.Options{
Scheme: scheme,
ByObject: map[client.Object]cache.ByObject{
Expand Down Expand Up @@ -228,9 +232,33 @@ func main() { //nolint:funlen,maintidx
Field: fields.Set{"metadata.name": cluster.ClusterAuthenticationObj}.AsSelector(),
},
// for prometheus and black-box deployment and ones we owns
&appsv1.Deployment{}: {Namespaces: deploymentCache},
// kueue need prometheusrules
&promv1.PrometheusRule{}: {Namespaces: deploymentCache},
&appsv1.Deployment{}: {
Namespaces: oDHCache,
},
// kueue + monitoring need prometheusrules
&promv1.PrometheusRule{}: {
Namespaces: oDHCache,
},
&promv1.ServiceMonitor{}: {
Namespaces: oDHCache,
},
&routev1.Route{}: {
Namespaces: oDHCache,
},
&networkingv1.NetworkPolicy{}: {
Namespaces: oDHCache,
},
&rbacv1.Role{}: {
Namespaces: oDHCache,
},
&rbacv1.RoleBinding{}: {
Namespaces: oDHCache,
},
&rbacv1.ClusterRole{}: {},
&rbacv1.ClusterRoleBinding{}: {},
&securityv1.SecurityContextConstraints{}: {
Namespaces: oDHCache,
},
},
}

Expand Down Expand Up @@ -344,9 +372,6 @@ func main() { //nolint:funlen,maintidx
os.Exit(1)
}

// get old release version before we create default DSCI CR
oldReleaseVersion, _ := upgrade.GetDeployedRelease(ctx, setupClient)

// Check if user opted for disabling DSC configuration
disableDSCConfig, existDSCConfig := os.LookupEnv("DISABLE_DSC_CONFIG")
if existDSCConfig && disableDSCConfig != "false" {
Expand Down Expand Up @@ -414,11 +439,37 @@ func main() { //nolint:funlen,maintidx
}
}

func createSecretCacheConfig(platform cluster.Platform) map[string]cache.Config {
func createSecretCacheConfig(ctx context.Context, cli client.Client, upgrade bool, platform cluster.Platform) map[string]cache.Config {
namespaceConfigs := map[string]cache.Config{
"istio-system": {}, // for both knative-serving-cert and default-modelregistry-cert,as an easy workarond, to watch all in this namespace for now
"istio-system": {}, // for both knative-serving-cert and default-modelregistry-cert, as an easy workarond, to watch both in this namespace
"openshift-ingress": {},
}
// upgrade cache
if upgrade {
// TODO: if we dont want harcoded above two namespace we can add them with label selector
// maistra.io/member-of=istio-system
// network.openshift.io/policy-group=ingress

labelSelector := client.MatchingLabels{
"opendatahub.io/generated-namespace": "true",
}
namespaceList := &corev1.NamespaceList{}
if err := cli.List(ctx, namespaceList, labelSelector); err != nil {
// return application (+ monitoring + default wb ) namespace
return namespaceConfigs
}

for _, ns := range namespaceList.Items {
namespaceConfigs[ns.Name] = cache.Config{}
}
// on managed, we keep operator namespace fixed
if platform == cluster.ManagedRhoai {
namespaceConfigs["redhat-ods-operator"] = cache.Config{}
}
return namespaceConfigs
}

// clean install cache
switch platform {
case cluster.ManagedRhoai:
namespaceConfigs["redhat-ods-monitoring"] = cache.Config{}
Expand All @@ -433,11 +484,40 @@ func createSecretCacheConfig(platform cluster.Platform) map[string]cache.Config
default:
namespaceConfigs["opendatahub"] = cache.Config{}
}
// if user create namespace and want it to be used as application namespace
// they need to create label "opendatahub.io/watched-namespace": "true" then set DSCI to use it
labelSelector := client.MatchingLabels{
"opendatahub.io/watched-namespace": "true",
}
namespaceList := &corev1.NamespaceList{}
if err := cli.List(ctx, namespaceList, labelSelector); err != nil {
return namespaceConfigs
}
for _, ns := range namespaceList.Items {
namespaceConfigs[ns.Name] = cache.Config{}
}
return namespaceConfigs
}

func createDeploymentCacheConfig(platform cluster.Platform) map[string]cache.Config {
func createODHGeneralCacheConfig(ctx context.Context, cli client.Client, upgrade bool, platform cluster.Platform) map[string]cache.Config {
namespaceConfigs := map[string]cache.Config{}
// upgrade cache
if upgrade {
labelSelector := client.MatchingLabels{
"opendatahub.io/generated-namespace": "true",
}
namespaceList := &corev1.NamespaceList{}
if err := cli.List(ctx, namespaceList, labelSelector); err != nil {
return namespaceConfigs
}
for _, ns := range namespaceList.Items {
namespaceConfigs[ns.Name] = cache.Config{}
}
// remove rhods-notebooks if it exists since we do not have deployment in this namespace, only SFS
delete(namespaceConfigs, cluster.DefaultNotebooksNamespace)
return namespaceConfigs
}
// clean install cache
switch platform {
case cluster.ManagedRhoai: // no need workbench NS, only SFS no Deployment
namespaceConfigs["redhat-ods-monitoring"] = cache.Config{}
Expand All @@ -447,6 +527,18 @@ func createDeploymentCacheConfig(platform cluster.Platform) map[string]cache.Con
default:
namespaceConfigs["opendatahub"] = cache.Config{}
}
// if user create namespace and want it to be used as application namespace
// they need to create label "opendatahub.io/watched-namespace": "true" then set DSCI to use it
labelSelector := client.MatchingLabels{
"opendatahub.io/watched-namespace": "true",
}
namespaceList := &corev1.NamespaceList{}
if err := cli.List(ctx, namespaceList, labelSelector); err != nil {
return namespaceConfigs
}
for _, ns := range namespaceList.Items {
namespaceConfigs[ns.Name] = cache.Config{}
}
return namespaceConfigs
}

Expand Down
Loading