Files
k3k/pkg/controller/util/util.go
Michael Bolot 9faab4f82d Changing the cluster to be namespaced (#110)
* Changing the cluster to be namespaced

Changes the cluster type to be namespaced (and changes the various
controllers to work with this new feature). Also adds crd generation and
docs to the core cluster type.

* CI fix
2024-09-05 22:50:11 +03:00

87 lines
1.9 KiB
Go

package util
import (
"context"
"fmt"
"github.com/rancher/k3k/pkg/apis/k3k.io/v1alpha1"
v1 "k8s.io/api/core/v1"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
namespacePrefix = "k3k-"
k3SImageName = "rancher/k3s"
AdminCommonName = "system:admin"
ServerPort = 6443
)
const (
K3kSystemNamespace = namespacePrefix + "system"
)
func ClusterNamespace(cluster *v1alpha1.Cluster) string {
return cluster.Namespace
}
func ServerSvcName(cluster *v1alpha1.Cluster) string {
return fmt.Sprintf("k3k-%s-service", cluster.Name)
}
func ServerConfigName(cluster *v1alpha1.Cluster) string {
return fmt.Sprintf("k3k-%s-server-config", cluster.Name)
}
func ServerInitConfigName(cluster *v1alpha1.Cluster) string {
return fmt.Sprintf("k3k-init-%s-server-config", cluster.Name)
}
func AgentConfigName(cluster *v1alpha1.Cluster) string {
return fmt.Sprintf("k3k-%s-agent-config", cluster.Name)
}
func K3SImage(cluster *v1alpha1.Cluster) string {
return k3SImageName + ":" + cluster.Spec.Version
}
func LogAndReturnErr(errString string, err error) error {
klog.Errorf("%s: %v", errString, err)
return err
}
func nodeAddress(node *v1.Node) string {
var externalIP string
var internalIP string
for _, ip := range node.Status.Addresses {
if ip.Type == "ExternalIP" && ip.Address != "" {
externalIP = ip.Address
break
}
if ip.Type == "InternalIP" && ip.Address != "" {
internalIP = ip.Address
}
}
if externalIP != "" {
return externalIP
}
return internalIP
}
// return all the nodes external addresses, if not found then return internal addresses
func Addresses(ctx context.Context, client client.Client) ([]string, error) {
var nodeList v1.NodeList
if err := client.List(ctx, &nodeList); err != nil {
return nil, err
}
var addresses []string
for _, node := range nodeList.Items {
addresses = append(addresses, nodeAddress(&node))
}
return addresses, nil
}