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

misc: Env with label #136

Merged
merged 5 commits into from
Feb 3, 2025
Merged
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
43 changes: 41 additions & 2 deletions common-lib/utils/k8s/K8sUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type K8sService interface {
GetConfigMapWithCtx(ctx context.Context, namespace string, name string, client *v12.CoreV1Client) (*v1.ConfigMap, error)
GetNsIfExists(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, exists bool, err error)
CreateNsIfNotExists(namespace string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error)
CreateNsWithLabelsIfNotExists(namespace string, labels map[string]string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error)
UpdateNSLabels(namespace *v1.Namespace, labels map[string]string, clusterConfig *ClusterConfig) (ns *v1.Namespace, err error)
GetK8sDiscoveryClientInCluster() (*discovery.DiscoveryClient, error)
GetK8sDiscoveryClient(clusterConfig *ClusterConfig) (*discovery.DiscoveryClient, error)
Expand Down Expand Up @@ -144,6 +145,7 @@ type K8sService interface {
//below functions are exposed for K8sUtilExtended
GetRestConfigByClusterWithoutCustomTransport(clusterConfig *ClusterConfig) (*rest.Config, error)
OverrideRestConfigWithCustomTransport(restConfig *rest.Config) (*rest.Config, error)
CreateNsWithLabels(namespace string, labels map[string]string, client *v12.CoreV1Client) (ns *v1.Namespace, err error)
CreateNs(namespace string, client *v12.CoreV1Client) (ns *v1.Namespace, err error)
}

Expand Down Expand Up @@ -299,12 +301,12 @@ func (impl *K8sServiceImpl) CreateNsIfNotExists(namespace string, clusterConfig
}
ns, exists, err := impl.GetNsIfExists(namespace, v12Client)
if err != nil {
impl.logger.Errorw("error", "error", err, "clusterConfig", clusterConfig)
impl.logger.Errorw("error", "error", err)
return ns, false, err
}
if exists {
nsCreated = false
impl.logger.Infow("namesapce already exist")
impl.logger.Infow("namespace already exist", "namespace", namespace)
return ns, nsCreated, nil
}
impl.logger.Infow("ns not exists creating", "ns", namespace)
Expand All @@ -317,6 +319,32 @@ func (impl *K8sServiceImpl) CreateNsIfNotExists(namespace string, clusterConfig
return ns, nsCreated, err
}

func (impl *K8sServiceImpl) CreateNsWithLabelsIfNotExists(namespace string, labels map[string]string, clusterConfig *ClusterConfig) (ns *v1.Namespace, nsCreated bool, err error) {
v12Client, err := impl.GetCoreV1Client(clusterConfig)
if err != nil {
impl.logger.Errorw("error", "error", err, "clusterConfig", clusterConfig)
return nil, false, err
}
ns, exists, err := impl.GetNsIfExists(namespace, v12Client)
if err != nil {
impl.logger.Errorw("error", "error", err)
return ns, false, err
}
if exists {
nsCreated = false
impl.logger.Infow("namespace already exist", "namespace", namespace)
return ns, nsCreated, nil
}
impl.logger.Infow("ns not exists creating", "ns", namespace)
ns, err = impl.CreateNsWithLabels(namespace, labels, v12Client)
if err != nil {
impl.logger.Errorw("error in creating ns", "namespace", namespace, "err", err)
return nil, false, err
}
nsCreated = true
return ns, nsCreated, err
}

func (impl *K8sServiceImpl) UpdateNSLabels(namespace *v1.Namespace, labels map[string]string, clusterConfig *ClusterConfig) (ns *v1.Namespace, err error) {
v12Client, err := impl.GetCoreV1Client(clusterConfig)
if err != nil {
Expand Down Expand Up @@ -359,6 +387,17 @@ func (impl *K8sServiceImpl) CreateNs(namespace string, client *v12.CoreV1Client)
}
}

func (impl *K8sServiceImpl) CreateNsWithLabels(namespace string, labels map[string]string, client *v12.CoreV1Client) (ns *v1.Namespace, err error) {
nsSpec := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace, Labels: labels}}
ns, err = client.Namespaces().Create(context.Background(), nsSpec, metav1.CreateOptions{})
if err != nil {
impl.logger.Errorw("error in creating ns", "err", err)
return nil, err
} else {
return ns, nil
}
}

func (impl *K8sServiceImpl) deleteNs(namespace string, client *v12.CoreV1Client) error {
err := client.Namespaces().Delete(context.Background(), namespace, metav1.DeleteOptions{})
return err
Expand Down