Skip to content

Commit

Permalink
fix: only register Sqlinstance informer in gcp clusters
Browse files Browse the repository at this point in the history
Co-authored-by: ybelmekk <[email protected]>
Co-authored-by: christeredvartsen <[email protected]>
  • Loading branch information
3 people committed Apr 11, 2024
1 parent d372a52 commit dd0edf0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
9 changes: 9 additions & 0 deletions internal/k8s/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ type StaticCluster struct {
Host string
Token string
}

func (c *Config) IsStaticCluster(cluster string) bool {
for _, sc := range c.StaticClusters {
if sc.Name == cluster {
return true
}
}
return false
}
16 changes: 12 additions & 4 deletions internal/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

sql_cnrm_cloud_google_com_v1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/sql/v1beta1"
"github.com/google/uuid"
"github.com/nais/api/internal/auth/authz"
"github.com/nais/api/internal/database"
Expand All @@ -16,7 +17,6 @@ import (
kafka_nais_io_v1 "github.com/nais/liberator/pkg/apis/kafka.nais.io/v1"
naisv1 "github.com/nais/liberator/pkg/apis/nais.io/v1"
naisv1alpha1 "github.com/nais/liberator/pkg/apis/nais.io/v1alpha1"
sql_cnrm_cloud_google_com_v1beta1 "github.com/nais/liberator/pkg/apis/sql.cnrm.cloud.google.com/v1beta1"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -44,7 +44,12 @@ func (c ClusterInformers) Start(ctx context.Context, log logrus.FieldLogger) err
go informer.AppInformer.Informer().Run(ctx.Done())
go informer.NaisjobInformer.Informer().Run(ctx.Done())
go informer.JobInformer.Informer().Run(ctx.Done())
go informer.SqlInstanceInformer.Informer().Run(ctx.Done())
if informer.SqlInstanceInformer != nil {
go informer.SqlInstanceInformer.Informer().Run(ctx.Done())
}
if informer.SqlDatabaseInformer != nil {
go informer.SqlDatabaseInformer.Informer().Run(ctx.Done())
}
go informer.SqlDatabaseInformer.Informer().Run(ctx.Done())
if informer.TopicInformer != nil {
go informer.TopicInformer.Informer().Run(ctx.Done())
Expand Down Expand Up @@ -193,8 +198,11 @@ func New(tenant string, cfg Config, db Database, log logrus.FieldLogger, opts ..
infs[cluster].AppInformer = dinf.ForResource(naisv1alpha1.GroupVersion.WithResource("applications"))
infs[cluster].NaisjobInformer = dinf.ForResource(naisv1.GroupVersion.WithResource("naisjobs"))
infs[cluster].JobInformer = inf.Batch().V1().Jobs()
infs[cluster].SqlInstanceInformer = dinf.ForResource(sql_cnrm_cloud_google_com_v1beta1.GroupVersion.WithResource("sqlinstances"))
infs[cluster].SqlDatabaseInformer = dinf.ForResource(sql_cnrm_cloud_google_com_v1beta1.GroupVersion.WithResource("sqldatabases"))

if !cfg.IsStaticCluster(cluster) {
infs[cluster].SqlInstanceInformer = dinf.ForResource(sql_cnrm_cloud_google_com_v1beta1.SchemeGroupVersion.WithResource("sqlinstances"))
infs[cluster].SqlDatabaseInformer = dinf.ForResource(sql_cnrm_cloud_google_com_v1beta1.SchemeGroupVersion.WithResource("sqldatabases"))
}

clientSets[cluster] = clients{
client: clientSet,
Expand Down
8 changes: 8 additions & 0 deletions internal/sqlinstance/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func (c *Client) SqlInstance(ctx context.Context, env string, teamSlug slug.Slug
return nil, fmt.Errorf("unknown env: %s", env)
}

if inf.SqlInstanceInformer == nil {
return nil, fmt.Errorf("SQL instance informer not supported in env: %q", env)
}

instance, err := inf.SqlInstanceInformer.Lister().ByNamespace(string(teamSlug)).Get(instanceName)
if err != nil {
return nil, fmt.Errorf("get SQL instance: %w", err)
Expand All @@ -33,6 +37,10 @@ func (c *Client) SqlInstances(ctx context.Context, teamSlug slug.Slug) ([]*model
ret := make([]*model.SQLInstance, 0)

for env, infs := range c.informers {
if infs.SqlInstanceInformer == nil {
continue
}

objs, err := infs.SqlInstanceInformer.Lister().ByNamespace(string(teamSlug)).List(labels.Everything())
if err != nil {
return nil, c.error(ctx, err, "listing SQL instances")
Expand Down

0 comments on commit dd0edf0

Please sign in to comment.