Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
Add namespace option
Browse files Browse the repository at this point in the history
  • Loading branch information
ubombar committed Jul 21, 2023
1 parent 4d0c63c commit 657de57
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 49 deletions.
15 changes: 8 additions & 7 deletions pkg/fedmanctl/cmd/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ var managerCmd = &cobra.Command{
}

var managerFederateCmd = &cobra.Command{
Use: "federate <token>",
Use: "federate <token> <namespace>",
Short: "Federate a worker cluster with the generated token.",
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
f, err := fedmanctl.NewFedmanctl(kubeconfig, context, true)

if err != nil {
panic(err.Error())
}

err = f.FederateToManagerCluster(args[0])
err = f.FederateToManagerCluster(args[0], args[1])

if err != nil {
panic(err.Error())
Expand All @@ -36,9 +36,9 @@ var managerFederateCmd = &cobra.Command{
}

var managerUnfederateCmd = &cobra.Command{
Use: "unfederate <uid>",
Use: "unfederate <uid> <namespace>",
Short: "Unfederate a worker cluster with the uid.",
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
f, err := fedmanctl.NewFedmanctl(kubeconfig, context, true)

Expand All @@ -51,7 +51,7 @@ var managerUnfederateCmd = &cobra.Command{
// If user inputs cluster-XXX format, convert it to normal uid
clusterUID = strings.Replace(clusterUID, "cluster-", "", 1)

err = f.UnfederateFromManagerCluster(clusterUID)
err = f.UnfederateFromManagerCluster(clusterUID, args[1])

if err != nil {
panic(err.Error())
Expand Down Expand Up @@ -80,9 +80,10 @@ var managerListCmd = &cobra.Command{
if len(clusters) == 0 {
fmt.Println("no worker clusters available")
} else {
fmt.Printf("%v\t%v\n", "Cluster Name", "Cluster Namespace")
// Just display the uids for now
for _, cluster := range clusters {
fmt.Printf("%v\n", cluster.ObjectMeta.Name)
fmt.Printf("%v\t%v\n", cluster.ObjectMeta.Name, cluster.ObjectMeta.Namespace)
}
}
},
Expand Down
59 changes: 17 additions & 42 deletions pkg/fedmanctl/fedmanctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ type Fedmanctl interface {
GenerateWorkerClusterToken(clusterIP, clusterPort, visibility string, debug bool, labels map[string]string) (string, error)

// Link the worker cluster to the manager cluster. Configures the manager cluster by the token. Called by the manager context.
FederateToManagerCluster(token string) error
FederateToManagerCluster(token, namespace string) error

// Unlinks the worker cluster from manager cluster. Called by the manager context.
UnfederateFromManagerCluster(uid string) error
UnfederateFromManagerCluster(uid, namespace string) error

// List the worker cluster objects
ListWorkerClusters() ([]federationv1alpha1.Cluster, error)
Expand Down Expand Up @@ -254,47 +254,20 @@ func (f fedmanctl) GenerateWorkerClusterToken(clusterIP, clusterPort, visibility
}

// Link the worker cluster to the manager cluster. Configures the manager cluster by the token. Called by the manager context.
func (f fedmanctl) FederateToManagerCluster(token string) error {
func (f fedmanctl) FederateToManagerCluster(token, namespace string) error {
workerClusterInfo, err := DetokenizeWorkerClusterInfo(token)

if err != nil {
return err
}

// Create the federated namespace
federationUID, err := f.getClusterUID()
federationNamespaceName := namespace

if err != nil {
return err
}

federationNamespaceName := fmt.Sprintf(federationv1alpha1.FederationManagerNamespace, federationUID)
// Test if namespace exists
_, err = f.GetKubeClientset().CoreV1().Namespaces().Get(context.TODO(), federationNamespaceName, v1.GetOptions{})
if err != nil {
ns := &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: federationNamespaceName,
},
}
_, err = f.GetKubeClientset().CoreV1().Namespaces().Create(context.TODO(), ns, v1.CreateOptions{})

if err != nil {
return err
}
}

_, err = f.GetKubeClientset().CoreV1().Namespaces().Get(context.TODO(), "edgenet-federation", v1.GetOptions{})
if err != nil {
ns := &corev1.Namespace{
ObjectMeta: v1.ObjectMeta{
Name: "edgenet-federation",
},
}
_, err = f.GetKubeClientset().CoreV1().Namespaces().Create(context.TODO(), ns, v1.CreateOptions{})

if err != nil {
return err
}
return err
}

// secretName has the convention "secret-XXX" where XXX is the UID of the cluster
Expand All @@ -306,7 +279,7 @@ func (f fedmanctl) FederateToManagerCluster(token string) error {
secret := &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: secretName,
Namespace: "edgenet-federation",
Namespace: federationNamespaceName,
},
Data: map[string][]byte{
"ca.crt": []byte(workerClusterInfo.CACertificate),
Expand All @@ -315,7 +288,7 @@ func (f fedmanctl) FederateToManagerCluster(token string) error {
},
}

_, err = f.GetKubeClientset().CoreV1().Secrets("edgenet-federation").Create(context.TODO(), secret, v1.CreateOptions{})
_, err = f.GetKubeClientset().CoreV1().Secrets(federationNamespaceName).Create(context.TODO(), secret, v1.CreateOptions{})

if err != nil {
return err
Expand All @@ -324,7 +297,7 @@ func (f fedmanctl) FederateToManagerCluster(token string) error {
cluster := &federationv1alpha1.Cluster{
ObjectMeta: v1.ObjectMeta{
Name: clusterName,
Namespace: "edgenet-federation",
Namespace: federationNamespaceName,
Labels: workerClusterInfo.Labels,
},
Spec: federationv1alpha1.ClusterSpec{
Expand All @@ -338,30 +311,32 @@ func (f fedmanctl) FederateToManagerCluster(token string) error {
},
}

_, err = f.GetEdgeNetClientset().FederationV1alpha1().Clusters("edgenet-federation").Create(context.TODO(), cluster, v1.CreateOptions{})
_, err = f.GetEdgeNetClientset().FederationV1alpha1().Clusters(federationNamespaceName).Create(context.TODO(), cluster, v1.CreateOptions{})

if err != nil {
fmt.Println("NONOON")
return err
}

return nil
}

// Unlinks the worker cluster from manager cluster. Called by the manager context.
func (f fedmanctl) UnfederateToManagerCluster(uid string) error {
func (f fedmanctl) UnfederateToManagerCluster(uid, namespace string) error {
secretName := strings.Join([]string{"secret", uid}, "-")
clusterName := strings.Join([]string{"cluster", uid}, "-")

f.GetKubeClientset().CoreV1().Secrets("edgenet-federation").Delete(context.TODO(), secretName, v1.DeleteOptions{})
f.GetEdgeNetClientset().FederationV1alpha1().Clusters("edgenet-federation").Delete(context.TODO(), clusterName, v1.DeleteOptions{})
federationNamespaceName := namespace

f.GetKubeClientset().CoreV1().Secrets(federationNamespaceName).Delete(context.TODO(), secretName, v1.DeleteOptions{})
f.GetEdgeNetClientset().FederationV1alpha1().Clusters(federationNamespaceName).Delete(context.TODO(), clusterName, v1.DeleteOptions{})

return nil
}

// List the worker cluster objects
func (f fedmanctl) ListWorkerClusters() ([]federationv1alpha1.Cluster, error) {
clusters, err := f.GetEdgeNetClientset().FederationV1alpha1().Clusters("edgenet-federation").List(context.TODO(), v1.ListOptions{})
// Get all the cluster objects from all of the namespaces
clusters, err := f.GetEdgeNetClientset().FederationV1alpha1().Clusters("").List(context.TODO(), v1.ListOptions{})

if err != nil {
return nil, err
Expand Down

0 comments on commit 657de57

Please sign in to comment.