Skip to content
This repository has been archived by the owner on Sep 24, 2021. It is now read-only.

Commit

Permalink
use providerID that vaguely looks like a real provider id
Browse files Browse the repository at this point in the history
Signed-off-by: Chuck Ha <chuckh@vmware.com>
  • Loading branch information
chuckha committed Jun 18, 2019
1 parent b3ab4d6 commit b692739
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
15 changes: 2 additions & 13 deletions capkactuators/actuators.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,6 @@ import (
"sigs.k8s.io/kind/pkg/cluster/nodes"
)

func setKindName(machine *clusterv1.Machine, name string) {
a := machine.GetAnnotations()
a["name"] = name
machine.SetAnnotations(a)
}

func getKindName(machine *clusterv1.Machine) string {
annotations := machine.GetAnnotations()
return annotations["name"]
}

func getRole(machine *clusterv1.Machine) string {
// Figure out what kind of node we're making
labels := machine.GetLabels()
Expand Down Expand Up @@ -106,8 +95,8 @@ func kubeconfigToSecret(clusterName, namespace string) (*v1.Secret, error) {
Name: fmt.Sprintf("kubeconfig-%s", clusterName),
Namespace: namespace,
},
StringData: map[string]string{
"kubeconfig": string(data),
Data: map[string][]byte{
"kubeconfig": data,
},
}, nil
}
43 changes: 31 additions & 12 deletions capkactuators/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clu
fmt.Printf("%+v", err)
return err
}
setKindName(machine, controlPlaneNode.Name())
name := providerName(controlPlaneNode.Name())
machine.Spec.ProviderID = &name
return m.save(old, machine)
}

Expand All @@ -100,22 +101,23 @@ func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clu
}
controlPlaneNode, err := actions.CreateControlPlane(c.Name, lbip, machine.Spec.Versions.ControlPlane)
if err != nil {
fmt.Printf("%+v", err)
fmt.Printf("%+v\n", err)
return err
}
setKindName(machine, controlPlaneNode.Name())
name := providerName(controlPlaneNode.Name())
machine.Spec.ProviderID = &name
if err := m.save(old, machine); err != nil {
fmt.Printf("%+v", err)
fmt.Printf("%+v\n", err)
return err
}
s, err := kubeconfigToSecret(c.Name, c.Namespace)
if err != nil {
fmt.Printf("%+v", err)
fmt.Printf("%+v\n", err)
return err
}
// Save the secret to the management cluster
if _, err := m.Core.Secrets(machine.GetNamespace()).Create(s); err != nil {
fmt.Printf("%+v", err)
fmt.Printf("%+v\n", err)
return err
}
return nil
Expand All @@ -133,11 +135,12 @@ func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clu
fmt.Printf("%+v", err)
return err
}
setKindName(machine, worker.Name())
name := providerName(worker.Name())
machine.Spec.ProviderID = &name
return m.save(old, machine)
}
func (m *Machine) Delete(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) error {
return actions.DeleteNode(cluster.Name, getKindName(machine))
return actions.DeleteNode(cluster.Name, providerNameToLookupID(*machine.Spec.ProviderID))
}

func (m *Machine) Update(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) error {
Expand All @@ -146,15 +149,16 @@ func (m *Machine) Update(ctx context.Context, cluster *clusterv1.Cluster, machin
}

func (m *Machine) Exists(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) (bool, error) {
if getKindName(machine) == "" {
if machine.Spec.ProviderID == nil {
return false, nil
}
fmt.Println("Looking for a docker container named", getKindName(machine))
fmt.Println("Looking for a docker container named", providerNameToLookupID(*machine.Spec.ProviderID))
role := getRole(machine)
kindRole := CAPIroleToKindRole(role)
labels := []string{
fmt.Sprintf("label=%s=%s", constants.NodeRoleKey, role),
fmt.Sprintf("label=%s=%s", constants.NodeRoleKey, kindRole),
fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, cluster.Name),
fmt.Sprintf("name=^%s$", getKindName(machine)),
fmt.Sprintf("name=^%s$", providerNameToLookupID(*machine.Spec.ProviderID)),
}
fmt.Printf("using labels: %v\n", labels)
nodeList, err := nodes.List(labels...)
Expand Down Expand Up @@ -187,3 +191,18 @@ func (m *Machine) save(old, new *clusterv1.Machine) error {
}
return nil
}

func providerNameToLookupID(providerName string) string {
return providerName[1:]
}

func providerName(name string) string {
return fmt.Sprintf("/%s", name)
}

func CAPIroleToKindRole(CAPIRole string) string {
if CAPIRole == clusterAPIControlPlaneSetLabel {
return constants.ControlPlaneNodeRoleValue
}
return CAPIRole
}

0 comments on commit b692739

Please sign in to comment.