Maintenance: move all labels to constants and fix log formatting issues
This commit is contained in:
parent
178fe6d20d
commit
3cd1a05b91
@ -123,7 +123,7 @@ func parseCreateNodeCmd(cmd *cobra.Command, args []string) ([]*k3d.Node, *k3d.Cl
|
||||
Role: role,
|
||||
Image: image,
|
||||
Labels: map[string]string{
|
||||
"k3d.role": roleStr,
|
||||
k3d.LabelRole: roleStr,
|
||||
},
|
||||
}
|
||||
nodes = append(nodes, node)
|
||||
|
||||
@ -118,6 +118,6 @@ func printNodes(nodes []*k3d.Node, headersOff bool) {
|
||||
})
|
||||
|
||||
for _, node := range nodes {
|
||||
fmt.Fprintf(tabwriter, "%s\t%s\t%s\n", strings.TrimPrefix(node.Name, "/"), string(node.Role), node.Labels["k3d.cluster"])
|
||||
fmt.Fprintf(tabwriter, "%s\t%s\t%s\n", strings.TrimPrefix(node.Name, "/"), string(node.Role), node.Labels[k3d.LabelClusterName])
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,8 +101,8 @@ func CreateCluster(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus
|
||||
*/
|
||||
if !cluster.CreateClusterOpts.DisableImageVolume {
|
||||
imageVolumeName := fmt.Sprintf("%s-%s-images", k3d.DefaultObjectNamePrefix, cluster.Name)
|
||||
if err := runtime.CreateVolume(ctx, imageVolumeName, map[string]string{"k3d.cluster": cluster.Name}); err != nil {
|
||||
log.Errorln("Failed to create image volume '%s' for cluster '%s'", imageVolumeName, cluster.Name)
|
||||
if err := runtime.CreateVolume(ctx, imageVolumeName, map[string]string{k3d.LabelClusterName: cluster.Name}); err != nil {
|
||||
log.Errorf("Failed to create image volume '%s' for cluster '%s'", imageVolumeName, cluster.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -127,10 +127,10 @@ func CreateCluster(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus
|
||||
if node.Labels == nil {
|
||||
node.Labels = make(map[string]string) // TODO: maybe create an init function?
|
||||
}
|
||||
node.Labels["k3d.cluster"] = cluster.Name
|
||||
node.Labels[k3d.LabelClusterName] = cluster.Name
|
||||
node.Env = append(node.Env, fmt.Sprintf("K3S_TOKEN=%s", cluster.Token))
|
||||
node.Labels[k3d.LabelToken] = cluster.Token
|
||||
node.Labels["k3d.cluster.url"] = connectionURL
|
||||
node.Labels[k3d.LabelClusterToken] = cluster.Token
|
||||
node.Labels[k3d.LabelClusterURL] = connectionURL
|
||||
|
||||
// append extra labels
|
||||
for k, v := range extraLabels {
|
||||
@ -387,7 +387,7 @@ func GetClusters(ctx context.Context, runtime k3drt.Runtime) ([]*k3d.Cluster, er
|
||||
for _, node := range nodes {
|
||||
clusterExists := false
|
||||
for _, cluster := range clusters {
|
||||
if node.Labels["k3d.cluster"] == cluster.Name { // TODO: handle case, where this label doesn't exist
|
||||
if node.Labels[k3d.LabelClusterName] == cluster.Name { // TODO: handle case, where this label doesn't exist
|
||||
cluster.Nodes = append(cluster.Nodes, node)
|
||||
clusterExists = true
|
||||
break
|
||||
@ -396,7 +396,7 @@ func GetClusters(ctx context.Context, runtime k3drt.Runtime) ([]*k3d.Cluster, er
|
||||
// cluster is not in the list yet, so we add it with the current node as its first member
|
||||
if !clusterExists {
|
||||
clusters = append(clusters, &k3d.Cluster{
|
||||
Name: node.Labels["k3d.cluster"],
|
||||
Name: node.Labels[k3d.LabelClusterName],
|
||||
Nodes: []*k3d.Node{node},
|
||||
})
|
||||
}
|
||||
@ -445,7 +445,7 @@ func populateClusterFieldsFromLabels(cluster *k3d.Cluster) error {
|
||||
|
||||
// get k3s cluster's token
|
||||
if cluster.Token == "" {
|
||||
if token, ok := node.Labels[k3d.LabelToken]; ok {
|
||||
if token, ok := node.Labels[k3d.LabelClusterToken]; ok {
|
||||
cluster.Token = token
|
||||
}
|
||||
}
|
||||
@ -457,7 +457,7 @@ func populateClusterFieldsFromLabels(cluster *k3d.Cluster) error {
|
||||
// GetCluster returns an existing cluster with all fields and node lists populated
|
||||
func GetCluster(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Cluster) (*k3d.Cluster, error) {
|
||||
// get nodes that belong to the selected cluster
|
||||
nodes, err := runtime.GetNodesByLabel(ctx, map[string]string{"k3d.cluster": cluster.Name})
|
||||
nodes, err := runtime.GetNodesByLabel(ctx, map[string]string{k3d.LabelClusterName: cluster.Name})
|
||||
if err != nil {
|
||||
log.Errorf("Failed to get nodes for cluster '%s'", cluster.Name)
|
||||
}
|
||||
@ -599,4 +599,4 @@ func SortClusters(clusters []*k3d.Cluster) []*k3d.Cluster {
|
||||
return clusters[i].Name < clusters[j].Name
|
||||
})
|
||||
return clusters
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,7 +113,7 @@ func GetAndWriteKubeConfig(ctx context.Context, runtime runtimes.Runtime, cluste
|
||||
func GetKubeconfig(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Cluster) (*clientcmdapi.Config, error) {
|
||||
// get all master nodes for the selected cluster
|
||||
// TODO: getKubeconfig: we should make sure, that the master node we're trying to fetch from is actually running
|
||||
masterNodes, err := runtime.GetNodesByLabel(ctx, map[string]string{"k3d.cluster": cluster.Name, "k3d.role": string(k3d.MasterRole)})
|
||||
masterNodes, err := runtime.GetNodesByLabel(ctx, map[string]string{k3d.LabelClusterName: cluster.Name, k3d.LabelRole: string(k3d.MasterRole)})
|
||||
if err != nil {
|
||||
log.Errorln("Failed to get master nodes")
|
||||
return nil, err
|
||||
@ -129,11 +129,11 @@ func GetKubeconfig(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.C
|
||||
APIHost := k3d.DefaultAPIHost
|
||||
|
||||
for _, master := range masterNodes {
|
||||
if _, ok := master.Labels["k3d.master.api.port"]; ok {
|
||||
if _, ok := master.Labels[k3d.LabelMasterAPIPort]; ok {
|
||||
chosenMaster = master
|
||||
APIPort = master.Labels["k3d.master.api.port"]
|
||||
if _, ok := master.Labels["k3d.master.api.host"]; ok {
|
||||
APIHost = master.Labels["k3d.master.api.host"]
|
||||
APIPort = master.Labels[k3d.LabelMasterAPIPort]
|
||||
if _, ok := master.Labels[k3d.LabelMasterAPIHost]; ok {
|
||||
APIHost = master.Labels[k3d.LabelMasterAPIHost]
|
||||
}
|
||||
break
|
||||
}
|
||||
@ -228,7 +228,7 @@ func WriteKubeConfigToPath(ctx context.Context, kubeconfig *clientcmdapi.Config,
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debugf("Wrote kubeconfig to '%s'", output.Name)
|
||||
log.Debugf("Wrote kubeconfig to '%s'", output.Name())
|
||||
|
||||
return nil
|
||||
|
||||
|
||||
@ -38,7 +38,7 @@ func UpdateLoadbalancerConfig(ctx context.Context, runtime runtimes.Runtime, clu
|
||||
// update cluster details to ensure that we have the latest node list
|
||||
cluster, err = GetCluster(ctx, runtime, cluster)
|
||||
if err != nil {
|
||||
log.Errorln("Failed to update details for cluster '%s'", cluster.Name)
|
||||
log.Errorf("Failed to update details for cluster '%s'", cluster.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@ -51,7 +51,7 @@ func AddNodeToCluster(ctx context.Context, runtime runtimes.Runtime, node *k3d.N
|
||||
// skeleton
|
||||
if node.Labels == nil {
|
||||
node.Labels = map[string]string{
|
||||
"k3d.role": string(node.Role),
|
||||
k3d.LabelRole: string(node.Role),
|
||||
}
|
||||
}
|
||||
node.Env = []string{}
|
||||
@ -102,7 +102,7 @@ func AddNodeToCluster(ctx context.Context, runtime runtimes.Runtime, node *k3d.N
|
||||
}
|
||||
}
|
||||
if !k3sURLFound {
|
||||
if url, ok := node.Labels["k3d.cluster.url"]; ok {
|
||||
if url, ok := node.Labels[k3d.LabelClusterURL]; ok {
|
||||
node.Env = append(node.Env, fmt.Sprintf("K3S_URL=%s", url))
|
||||
} else {
|
||||
log.Warnln("Failed to find K3S_URL value!")
|
||||
@ -225,7 +225,7 @@ func CreateNode(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node, c
|
||||
}
|
||||
node.Labels = labels
|
||||
// second most important: the node role label
|
||||
node.Labels["k3d.role"] = string(node.Role)
|
||||
node.Labels[k3d.LabelRole] = string(node.Role)
|
||||
|
||||
// ### Environment ###
|
||||
node.Env = append(node.Env, k3d.DefaultNodeEnv...) // append default node env vars
|
||||
@ -258,7 +258,7 @@ func DeleteNode(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node) e
|
||||
log.Error(err)
|
||||
}
|
||||
|
||||
cluster, err := GetCluster(ctx, runtime, &k3d.Cluster{Name: node.Labels["k3d.cluster"]})
|
||||
cluster, err := GetCluster(ctx, runtime, &k3d.Cluster{Name: node.Labels[k3d.LabelClusterName]})
|
||||
if err != nil {
|
||||
log.Errorf("Failed to update loadbalancer: Failed to find cluster for node '%s'", node.Name)
|
||||
return err
|
||||
@ -293,9 +293,9 @@ func patchMasterSpec(node *k3d.Node) error {
|
||||
|
||||
// Add labels and TLS SAN for the exposed API
|
||||
// FIXME: For now, the labels concerning the API on the master nodes are only being used for configuring the kubeconfig
|
||||
node.Labels["k3d.master.api.hostIP"] = node.MasterOpts.ExposeAPI.HostIP // TODO: maybe get docker machine IP here
|
||||
node.Labels["k3d.master.api.host"] = node.MasterOpts.ExposeAPI.Host
|
||||
node.Labels["k3d.master.api.port"] = node.MasterOpts.ExposeAPI.Port
|
||||
node.Labels[k3d.LabelMasterAPIHostIP] = node.MasterOpts.ExposeAPI.HostIP // TODO: maybe get docker machine IP here
|
||||
node.Labels[k3d.LabelMasterAPIHost] = node.MasterOpts.ExposeAPI.Host
|
||||
node.Labels[k3d.LabelMasterAPIPort] = node.MasterOpts.ExposeAPI.Port
|
||||
|
||||
node.Args = append(node.Args, "--tls-san", node.MasterOpts.ExposeAPI.Host) // add TLS SAN for non default host name
|
||||
|
||||
|
||||
@ -91,7 +91,7 @@ func (d Containerd) DeleteNode(ctx context.Context, node *k3d.Node) error {
|
||||
return err
|
||||
}
|
||||
if err = container.Delete(ctx, []containerd.DeleteOpts{}...); err != nil {
|
||||
log.Errorln("Failed to delete container", container.ID)
|
||||
log.Errorf("Failed to delete container '%s'", container.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@ -182,7 +182,7 @@ func getContainerDetails(ctx context.Context, containerID string) (types.Contain
|
||||
|
||||
containerDetails, err := docker.ContainerInspect(ctx, containerID)
|
||||
if err != nil {
|
||||
log.Errorln("Failed to get details for container '%s'", containerID)
|
||||
log.Errorf("Failed to get details for container '%s'", containerID)
|
||||
return types.ContainerJSON{}, err
|
||||
}
|
||||
|
||||
@ -301,7 +301,7 @@ func (d Docker) ExecInNode(ctx context.Context, node *k3d.Node, cmd []string) er
|
||||
// get info about exec process inside container
|
||||
execInfo, err := docker.ContainerExecInspect(ctx, exec.ID)
|
||||
if err != nil {
|
||||
log.Errorln("Failed to inspect exec process in node '%s'", node.Name)
|
||||
log.Errorf("Failed to inspect exec process in node '%s'", node.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -322,7 +322,7 @@ func (d Docker) ExecInNode(ctx context.Context, node *k3d.Node, cmd []string) er
|
||||
|
||||
logs, err := ioutil.ReadAll(execConnection.Reader)
|
||||
if err != nil {
|
||||
log.Errorln("Failed to get logs from node '%s'", node.Name)
|
||||
log.Errorf("Failed to get logs from node '%s'", node.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@ -86,7 +86,7 @@ func TranslateNodeToContainer(node *k3d.Node) (*NodeInDocker, error) {
|
||||
/* Ports */
|
||||
exposedPorts, portBindings, err := nat.ParsePortSpecs(node.Ports)
|
||||
if err != nil {
|
||||
log.Errorln("Failed to parse port specs '%v'", node.Ports)
|
||||
log.Errorf("Failed to parse port specs '%v'", node.Ports)
|
||||
return nil, err
|
||||
}
|
||||
containerConfig.ExposedPorts = exposedPorts
|
||||
@ -116,7 +116,7 @@ func TranslateContainerToNode(cont *types.Container) (*k3d.Node, error) {
|
||||
Name: strings.TrimPrefix(cont.Names[0], "/"), // container name with leading '/' cut off
|
||||
Image: cont.Image,
|
||||
Labels: cont.Labels,
|
||||
Role: k3d.NodeRoles[cont.Labels["k3d.role"]],
|
||||
Role: k3d.NodeRoles[cont.Labels[k3d.LabelRole]],
|
||||
// TODO: all the rest
|
||||
}
|
||||
return node, nil
|
||||
@ -142,7 +142,7 @@ func TranslateContainerDetailsToNode(containerDetails types.ContainerJSON) (*k3d
|
||||
// get the clusterNetwork
|
||||
clusterNetwork := ""
|
||||
for networkName := range containerDetails.NetworkSettings.Networks {
|
||||
if strings.HasPrefix(networkName, fmt.Sprintf("%s-%s", k3d.DefaultObjectNamePrefix, containerDetails.Config.Labels["k3d.cluster"])) { // FIXME: catch error if label 'k3d.cluster' does not exist, but this should also never be the case
|
||||
if strings.HasPrefix(networkName, fmt.Sprintf("%s-%s", k3d.DefaultObjectNamePrefix, containerDetails.Config.Labels[k3d.LabelClusterName])) { // FIXME: catch error if label 'k3d.cluster' does not exist, but this should also never be the case
|
||||
clusterNetwork = networkName
|
||||
}
|
||||
}
|
||||
@ -150,16 +150,11 @@ func TranslateContainerDetailsToNode(containerDetails types.ContainerJSON) (*k3d
|
||||
// masterOpts
|
||||
masterOpts := k3d.MasterOpts{IsInit: false}
|
||||
for k, v := range containerDetails.Config.Labels {
|
||||
/*
|
||||
node.Labels["k3d.master.api.hostIP"] = node.MasterOpts.ExposeAPI.HostIP // TODO: maybe get docker machine IP here
|
||||
node.Labels["k3d.master.api.host"] = node.MasterOpts.ExposeAPI.Host
|
||||
node.Labels["k3d.master.api.port"] = node.MasterOpts.ExposeAPI.Port
|
||||
*/
|
||||
if k == "k3d.master.api.hostIP" {
|
||||
if k == k3d.LabelMasterAPIHostIP {
|
||||
masterOpts.ExposeAPI.HostIP = v
|
||||
} else if k == "k3d.master.api.host" {
|
||||
} else if k == k3d.LabelMasterAPIHost {
|
||||
masterOpts.ExposeAPI.Host = v
|
||||
} else if k == "k3d.master.api.port" {
|
||||
} else if k == k3d.LabelMasterAPIPort {
|
||||
masterOpts.ExposeAPI.Port = v
|
||||
}
|
||||
}
|
||||
@ -182,7 +177,7 @@ func TranslateContainerDetailsToNode(containerDetails types.ContainerJSON) (*k3d
|
||||
|
||||
node := &k3d.Node{
|
||||
Name: strings.TrimPrefix(containerDetails.Name, "/"), // container name with leading '/' cut off
|
||||
Role: k3d.NodeRoles[containerDetails.Config.Labels["k3d.role"]],
|
||||
Role: k3d.NodeRoles[containerDetails.Config.Labels[k3d.LabelRole]],
|
||||
Image: containerDetails.Image,
|
||||
Volumes: containerDetails.HostConfig.Binds,
|
||||
Env: env,
|
||||
|
||||
@ -45,7 +45,7 @@ func TestTranslateNodeToContainer(t *testing.T) {
|
||||
Args: []string{"--some-boolflag"},
|
||||
Ports: []string{"0.0.0.0:6443:6443/tcp"},
|
||||
Restart: true,
|
||||
Labels: map[string]string{"k3d.role": string(k3d.MasterRole), "test_key_1": "test_val_1"},
|
||||
Labels: map[string]string{k3d.LabelRole: string(k3d.MasterRole), "test_key_1": "test_val_1"},
|
||||
}
|
||||
|
||||
expectedRepresentation := &NodeInDocker{
|
||||
@ -54,7 +54,7 @@ func TestTranslateNodeToContainer(t *testing.T) {
|
||||
Image: "rancher/k3s:v0.9.0",
|
||||
Env: []string{"TEST_KEY_1=TEST_VAL_1"},
|
||||
Cmd: []string{"server", "--https-listen-port=6443", "--some-boolflag"},
|
||||
Labels: map[string]string{"k3d.role": string(k3d.MasterRole), "test_key_1": "test_val_1"},
|
||||
Labels: map[string]string{k3d.LabelRole: string(k3d.MasterRole), "test_key_1": "test_val_1"},
|
||||
ExposedPorts: nat.PortSet{},
|
||||
},
|
||||
HostConfig: container.HostConfig{
|
||||
|
||||
@ -39,7 +39,7 @@ func GetDefaultObjectLabelsFilter(clusterName string) filters.Args {
|
||||
for key, value := range k3d.DefaultObjectLabels {
|
||||
filters.Add("label", fmt.Sprintf("%s=%s", key, value))
|
||||
}
|
||||
filters.Add("label", fmt.Sprintf("k3d.cluster=%s", clusterName))
|
||||
filters.Add("label", fmt.Sprintf("%s=%s", k3d.LabelClusterName, clusterName))
|
||||
return filters
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ func (d Docker) CopyToNode(ctx context.Context, src string, dest string, node *k
|
||||
|
||||
container, err := getNodeContainer(ctx, node)
|
||||
if err != nil {
|
||||
log.Errorln("Failed to find container for target node '%s'", node.Name)
|
||||
log.Errorf("Failed to find container for target node '%s'", node.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@ -82,7 +82,7 @@ func (d Docker) DeleteVolume(ctx context.Context, name string) error {
|
||||
// check if volume is still in use
|
||||
if vol.UsageData != nil {
|
||||
if vol.UsageData.RefCount > 0 {
|
||||
log.Errorf("Failed to delete volume '%s'")
|
||||
log.Errorf("Failed to delete volume '%s'", vol.Name)
|
||||
return fmt.Errorf("Volume '%s' is still referenced by %d containers", name, vol.UsageData.RefCount)
|
||||
}
|
||||
}
|
||||
|
||||
@ -189,7 +189,7 @@ func LoadImagesIntoCluster(ctx context.Context, runtime runtimes.Runtime, images
|
||||
// delete tools container
|
||||
log.Infoln("Removing k3d-tools node...")
|
||||
if err := runtime.DeleteNode(ctx, toolsNode); err != nil {
|
||||
log.Errorln("Failed to delete tools node '%s': Try to delete it manually", toolsNode.Name)
|
||||
log.Errorf("Failed to delete tools node '%s': Try to delete it manually", toolsNode.Name)
|
||||
}
|
||||
|
||||
log.Infoln("Successfully imported image(s)")
|
||||
@ -210,7 +210,7 @@ func startToolsNode(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.
|
||||
Args: []string{"noop"},
|
||||
Labels: k3d.DefaultObjectLabels,
|
||||
}
|
||||
node.Labels["k3d.cluster"] = cluster.Name
|
||||
node.Labels[k3d.LabelClusterName] = cluster.Name
|
||||
if err := runtime.CreateNode(ctx, node); err != nil {
|
||||
log.Errorf("Failed to create tools container for cluster '%s'", cluster.Name)
|
||||
return node, err
|
||||
|
||||
@ -76,10 +76,16 @@ var DefaultObjectLabels = map[string]string{
|
||||
|
||||
// List of k3d technical label name
|
||||
const (
|
||||
LabelToken string = "k3d.cluster.token"
|
||||
LabelClusterName string = "k3d.cluster"
|
||||
LabelClusterURL string = "k3d.cluster.url"
|
||||
LabelClusterToken string = "k3d.cluster.token"
|
||||
LabelImageVolume string = "k3d.cluster.imageVolume"
|
||||
LabelNetworkExternal string = "k3d.cluster.network.external"
|
||||
LabelNetwork string = "k3d.cluster.network"
|
||||
LabelRole string = "k3d.role"
|
||||
LabelMasterAPIPort string = "k3d.master.api.port"
|
||||
LabelMasterAPIHost string = "k3d.master.api.host"
|
||||
LabelMasterAPIHostIP string = "k3d.master.api.hostIP"
|
||||
)
|
||||
|
||||
// DefaultRoleCmds maps the node roles to their respective default commands
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user