120 lines
2.9 KiB
Go
120 lines
2.9 KiB
Go
package templates
|
|
|
|
import (
|
|
"strings"
|
|
|
|
monov1alpha1 "example.com/monok8s/pkg/apis/monok8s/v1alpha1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
)
|
|
|
|
const DefaultNamespace = "kube-system"
|
|
|
|
func DefaultMonoKSConfig(v TemplateValues) monov1alpha1.MonoKSConfig {
|
|
return monov1alpha1.MonoKSConfig{
|
|
TypeMeta: metav1.TypeMeta{
|
|
APIVersion: monov1alpha1.APIVersion,
|
|
Kind: "MonoKSConfig",
|
|
},
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "example",
|
|
Namespace: DefaultNamespace,
|
|
},
|
|
Spec: monov1alpha1.MonoKSConfigSpec{
|
|
KubernetesVersion: v.KubernetesVersion,
|
|
NodeName: firstNonEmpty(v.NodeName, v.Hostname),
|
|
|
|
ClusterRole: v.ClusterRole,
|
|
InitControlPlane: v.InitControlPlane,
|
|
EnableControlAgent: v.EnableControlAgent,
|
|
|
|
ClusterName: v.ClusterName,
|
|
ClusterDomain: v.ClusterDomain,
|
|
|
|
PodSubnet: v.PodSubnet,
|
|
ServiceSubnet: v.ServiceSubnet,
|
|
|
|
APIServerAdvertiseAddress: v.APIServerAdvertiseAddress,
|
|
APIServerEndpoint: v.APIServerEndpoint,
|
|
|
|
BootstrapToken: v.BootstrapToken,
|
|
DiscoveryTokenCACertHash: v.DiscoveryTokenCACertHash,
|
|
|
|
ContainerRuntimeEndpoint: v.ContainerRuntimeEndpoint,
|
|
CNIPlugin: v.CNIPlugin,
|
|
|
|
AllowSchedulingOnControlPlane: v.AllowSchedulingOnControlPlane,
|
|
SkipImageCheck: v.SkipImageCheck,
|
|
|
|
KubeProxyNodePortAddresses: []string{
|
|
"primary",
|
|
},
|
|
|
|
SubjectAltNames: copyStringSlice(v.SubjectAltNames),
|
|
NodeLabels: copyStringMap(v.NodeLabels),
|
|
NodeAnnotations: copyStringMap(v.NodeAnnotations),
|
|
|
|
Network: monov1alpha1.NetworkSpec{
|
|
Hostname: firstNonEmpty(v.Hostname, v.NodeName),
|
|
ManagementIface: v.MgmtIface,
|
|
ManagementCIDR: v.MgmtAddress,
|
|
ManagementGW: v.MgmtGateway,
|
|
DNSNameservers: copyStringSlice(v.DNSNameservers),
|
|
DNSSearchDomains: copyStringSlice(v.DNSSearchDomains),
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func DefaultOSUpgrade(v TemplateValues) monov1alpha1.OSUpgrade {
|
|
return monov1alpha1.OSUpgrade{
|
|
TypeMeta: metav1.TypeMeta{
|
|
APIVersion: monov1alpha1.APIVersion,
|
|
Kind: "OSUpgrade",
|
|
},
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "example",
|
|
Namespace: DefaultNamespace,
|
|
},
|
|
Spec: monov1alpha1.OSUpgradeSpec{
|
|
DesiredVersion: v.KubernetesVersion,
|
|
NodeSelector: &metav1.LabelSelector{
|
|
MatchLabels: map[string]string{
|
|
"kubernetes.io/hostname": firstNonEmpty(v.NodeName, v.Hostname),
|
|
},
|
|
},
|
|
Catalog: &monov1alpha1.VersionCatalogSource{
|
|
URL: monov1alpha1.CatalogURL,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func firstNonEmpty(xs ...string) string {
|
|
for _, x := range xs {
|
|
if strings.TrimSpace(x) != "" {
|
|
return strings.TrimSpace(x)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func copyStringSlice(in []string) []string {
|
|
if len(in) == 0 {
|
|
return nil
|
|
}
|
|
out := make([]string, len(in))
|
|
copy(out, in)
|
|
return out
|
|
}
|
|
|
|
func copyStringMap(in map[string]string) map[string]string {
|
|
if len(in) == 0 {
|
|
return nil
|
|
}
|
|
out := make(map[string]string, len(in))
|
|
for k, v := range in {
|
|
out[k] = v
|
|
}
|
|
return out
|
|
}
|