github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/kube/teams.go (about)

     1  package kube
     2  
     3  import (
     4  	"sort"
     5  	"strings"
     6  
     7  	"github.com/olli-ai/jx/v2/pkg/kube/naming"
     8  	"github.com/pkg/errors"
     9  
    10  	v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
    11  	"github.com/jenkins-x/jx-api/pkg/client/clientset/versioned"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  	"k8s.io/client-go/kubernetes"
    14  )
    15  
    16  const adminNamespaceAnnotation = "jenkins-x.io/admin-namespace"
    17  
    18  // GetAdminNamespace tries to find the admin namespace that corresponds to this team.
    19  // in other words this is the namespace where the team CRD was initially created when this team was created,
    20  // or the current team namespace for the case where this team was just created with a standalone `jx install`
    21  func GetAdminNamespace(kubeClient kubernetes.Interface, teamNs string) (string, error) {
    22  	namespace, err := kubeClient.CoreV1().Namespaces().Get(teamNs, metav1.GetOptions{})
    23  	if err != nil {
    24  		return "", errors.Wrapf(err, "obtaining the team namespace (%s) when getting the admin namespace", teamNs)
    25  	}
    26  	adminNs := namespace.Annotations[adminNamespaceAnnotation]
    27  	if adminNs != "" {
    28  		return adminNs, nil
    29  	}
    30  	return teamNs, nil
    31  }
    32  
    33  // SetAdminNamespace annotates the given namespace with a backlink to the admin namespace.
    34  // it does not make any changes if the current annotation points to the same admin namespace
    35  func SetAdminNamespace(kubeClient kubernetes.Interface, teamNs string, adminNs string) error {
    36  	namespace, err := kubeClient.CoreV1().Namespaces().Get(teamNs, metav1.GetOptions{})
    37  	if err != nil {
    38  		return errors.Wrapf(err, "obtaining the namespace (%s) when updating the admin namespace", teamNs)
    39  	}
    40  	oldAdminNs := namespace.Annotations[adminNamespaceAnnotation]
    41  	if oldAdminNs == adminNs {
    42  		// nothing to do
    43  		return nil
    44  	}
    45  	namespace.Annotations[adminNamespaceAnnotation] = adminNs
    46  	// TODO use patch
    47  	_, err = kubeClient.CoreV1().Namespaces().Update(namespace)
    48  	return err
    49  }
    50  
    51  // GetPendingTeams returns the pending teams with the sorted order of names
    52  func GetPendingTeams(jxClient versioned.Interface, ns string) (map[string]*v1.Team, []string, error) {
    53  	m := map[string]*v1.Team{}
    54  
    55  	names := []string{}
    56  	teamList, err := jxClient.JenkinsV1().Teams(ns).List(metav1.ListOptions{})
    57  	if err != nil {
    58  		return m, names, err
    59  	}
    60  	for _, team := range teamList.Items {
    61  		n := team.Name
    62  		copy := team
    63  		m[n] = &copy
    64  		if n != "" {
    65  			names = append(names, n)
    66  		}
    67  	}
    68  	sort.Strings(names)
    69  	return m, names, nil
    70  }
    71  
    72  // CreateTeam creates a new default Team
    73  func CreateTeam(ns string, name string, members []string) *v1.Team {
    74  	kind := v1.TeamKindTypeCD
    75  	team := &v1.Team{
    76  		ObjectMeta: metav1.ObjectMeta{
    77  			Name:      naming.ToValidName(name),
    78  			Namespace: ns,
    79  		},
    80  		Spec: v1.TeamSpec{
    81  			Label:   strings.Title(name),
    82  			Members: members,
    83  			Kind:    kind,
    84  		},
    85  	}
    86  	return team
    87  }
    88  
    89  // DeleteTeam deletes the team resource but does not uninstall the underlying namespaces
    90  func DeleteTeam(jxClient versioned.Interface, ns string, teamName string) error {
    91  	teamInterface := jxClient.JenkinsV1().Teams(ns)
    92  	_, err := teamInterface.Get(teamName, metav1.GetOptions{})
    93  	if err == nil {
    94  		err = teamInterface.Delete(teamName, nil)
    95  	}
    96  	return err
    97  }