Refactor to clean up TemplateFunctions

We had some fields that were duplicated; this was confusing and seemed
likely to cause (subtle) bugs.
This commit is contained in:
Justin SB 2020-06-17 23:39:16 -04:00
parent 121c0aaa31
commit af09f50fef
5 changed files with 105 additions and 103 deletions

View File

@ -534,11 +534,8 @@ func (c *ApplyClusterCmd) Run(ctx context.Context) error {
} }
tf := &TemplateFunctions{ tf := &TemplateFunctions{
cluster: cluster, KopsModelContext: *modelContext,
instanceGroups: c.InstanceGroups, tags: clusterTags,
tags: clusterTags,
region: region,
modelContext: modelContext,
} }
l.Tags = clusterTags l.Tags = clusterTags
@ -771,7 +768,7 @@ func (c *ApplyClusterCmd) Run(ctx context.Context) error {
return fmt.Errorf("unknown cloudprovider %q", cluster.Spec.CloudProvider) return fmt.Errorf("unknown cloudprovider %q", cluster.Spec.CloudProvider)
} }
l.TemplateFunctions["Masters"] = tf.modelContext.MasterInstanceGroups l.TemplateFunctions["Masters"] = tf.MasterInstanceGroups
err = tf.AddTo(l.TemplateFunctions, secretStore) err = tf.AddTo(l.TemplateFunctions, secretStore)
if err != nil { if err != nil {

View File

@ -91,11 +91,10 @@ func runChannelBuilderTest(t *testing.T, key string, addonManifests []string) {
} }
tf := &TemplateFunctions{ tf := &TemplateFunctions{
cluster: cluster, KopsModelContext: model.KopsModelContext{
modelContext: &model.KopsModelContext{
Cluster: cluster, Cluster: cluster,
Region: "us-east-1",
}, },
region: "us-east-1",
} }
tf.AddTo(templates.TemplateFunctions, secretStore) tf.AddTo(templates.TemplateFunctions, secretStore)

View File

@ -258,13 +258,11 @@ func (c *populateClusterSpec) run(clientset simple.Clientset) error {
return err return err
} }
modelContext := &model.KopsModelContext{
Cluster: cluster,
}
tf := &TemplateFunctions{ tf := &TemplateFunctions{
cluster: cluster, KopsModelContext: model.KopsModelContext{
tags: tags, Cluster: cluster,
modelContext: modelContext, },
tags: tags,
} }
templateFunctions := make(template.FuncMap) templateFunctions := make(template.FuncMap)
@ -326,7 +324,7 @@ func (c *populateClusterSpec) run(clientset simple.Clientset) error {
fullCluster := &kopsapi.Cluster{} fullCluster := &kopsapi.Cluster{}
*fullCluster = *cluster *fullCluster = *cluster
fullCluster.Spec = *completed fullCluster.Spec = *completed
tf.cluster = fullCluster tf.Cluster = fullCluster
if errs := validation.ValidateCluster(fullCluster, true); len(errs) != 0 { if errs := validation.ValidateCluster(fullCluster, true); len(errs) != 0 {
return fmt.Errorf("Completed cluster failed validation: %v", errs.ToAggregate()) return fmt.Errorf("Completed cluster failed validation: %v", errs.ToAggregate())

View File

@ -54,22 +54,21 @@ import (
// TemplateFunctions provides a collection of methods used throughout the templates // TemplateFunctions provides a collection of methods used throughout the templates
type TemplateFunctions struct { type TemplateFunctions struct {
cluster *kops.Cluster model.KopsModelContext
instanceGroups []*kops.InstanceGroup tags sets.String
modelContext *model.KopsModelContext
region string
tags sets.String
} }
// AddTo defines the available functions we can use in our YAML models. // AddTo defines the available functions we can use in our YAML models.
// If we are trying to get a new function implemented it MUST // If we are trying to get a new function implemented it MUST
// be defined here. // be defined here.
func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretStore) (err error) { func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretStore) (err error) {
cluster := tf.Cluster
dest["EtcdScheme"] = tf.EtcdScheme dest["EtcdScheme"] = tf.EtcdScheme
dest["SharedVPC"] = tf.SharedVPC dest["SharedVPC"] = tf.SharedVPC
dest["ToJSON"] = tf.ToJSON dest["ToJSON"] = tf.ToJSON
dest["UseBootstrapTokens"] = tf.modelContext.UseBootstrapTokens dest["UseBootstrapTokens"] = tf.UseBootstrapTokens
dest["UseEtcdTLS"] = tf.modelContext.UseEtcdTLS dest["UseEtcdTLS"] = tf.UseEtcdTLS
// Remember that we may be on a different arch from the target. Hard-code for now. // Remember that we may be on a different arch from the target. Hard-code for now.
dest["Arch"] = func() string { return "amd64" } dest["Arch"] = func() string { return "amd64" }
dest["replace"] = func(s, find, replace string) string { dest["replace"] = func(s, find, replace string) string {
@ -82,7 +81,7 @@ func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretS
sprigTxtFuncMap := sprig.TxtFuncMap() sprigTxtFuncMap := sprig.TxtFuncMap()
dest["indent"] = sprigTxtFuncMap["indent"] dest["indent"] = sprigTxtFuncMap["indent"]
dest["ClusterName"] = tf.modelContext.ClusterName dest["ClusterName"] = tf.ClusterName
dest["HasTag"] = tf.HasTag dest["HasTag"] = tf.HasTag
dest["WithDefaultBool"] = func(v *bool, defaultValue bool) bool { dest["WithDefaultBool"] = func(v *bool, defaultValue bool) bool {
if v != nil { if v != nil {
@ -92,22 +91,22 @@ func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretS
} }
dest["GetInstanceGroup"] = tf.GetInstanceGroup dest["GetInstanceGroup"] = tf.GetInstanceGroup
dest["CloudTags"] = tf.modelContext.CloudTagsForInstanceGroup dest["CloudTags"] = tf.CloudTagsForInstanceGroup
dest["KubeDNS"] = func() *kops.KubeDNSConfig { dest["KubeDNS"] = func() *kops.KubeDNSConfig {
return tf.cluster.Spec.KubeDNS return cluster.Spec.KubeDNS
} }
dest["NodeLocalDNSClusterIP"] = func() string { dest["NodeLocalDNSClusterIP"] = func() string {
if tf.cluster.Spec.KubeProxy.ProxyMode == "ipvs" { if cluster.Spec.KubeProxy.ProxyMode == "ipvs" {
return tf.cluster.Spec.KubeDNS.ServerIP return cluster.Spec.KubeDNS.ServerIP
} }
return "__PILLAR__CLUSTER__DNS__" return "__PILLAR__CLUSTER__DNS__"
} }
dest["NodeLocalDNSServerIP"] = func() string { dest["NodeLocalDNSServerIP"] = func() string {
if tf.cluster.Spec.KubeProxy.ProxyMode == "ipvs" { if cluster.Spec.KubeProxy.ProxyMode == "ipvs" {
return "" return ""
} }
return tf.cluster.Spec.KubeDNS.ServerIP return cluster.Spec.KubeDNS.ServerIP
} }
dest["NodeLocalDNSHealthCheck"] = func() string { dest["NodeLocalDNSHealthCheck"] = func() string {
return fmt.Sprintf("%d", wellknownports.NodeLocalDNSHealthCheck) return fmt.Sprintf("%d", wellknownports.NodeLocalDNSHealthCheck)
@ -121,7 +120,7 @@ func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretS
// TODO: Only for GCE? // TODO: Only for GCE?
dest["EncodeGCELabel"] = gce.EncodeGCELabel dest["EncodeGCELabel"] = gce.EncodeGCELabel
dest["Region"] = func() string { dest["Region"] = func() string {
return tf.region return tf.Region
} }
if featureflag.EnableExternalCloudController.Enabled() { if featureflag.EnableExternalCloudController.Enabled() {
@ -143,8 +142,8 @@ func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretS
} }
} }
if tf.cluster.Spec.Networking != nil && tf.cluster.Spec.Networking.Flannel != nil { if cluster.Spec.Networking != nil && cluster.Spec.Networking.Flannel != nil {
flannelBackendType := tf.cluster.Spec.Networking.Flannel.Backend flannelBackendType := cluster.Spec.Networking.Flannel.Backend
if flannelBackendType == "" { if flannelBackendType == "" {
klog.Warningf("Defaulting flannel backend to udp (not a recommended configuration)") klog.Warningf("Defaulting flannel backend to udp (not a recommended configuration)")
flannelBackendType = "udp" flannelBackendType = "udp"
@ -152,7 +151,7 @@ func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretS
dest["FlannelBackendType"] = func() string { return flannelBackendType } dest["FlannelBackendType"] = func() string { return flannelBackendType }
} }
if tf.cluster.Spec.Networking != nil && tf.cluster.Spec.Networking.Weave != nil { if cluster.Spec.Networking != nil && cluster.Spec.Networking.Weave != nil {
weavesecretString := "" weavesecretString := ""
weavesecret, _ := secretStore.Secret("weavepassword") weavesecret, _ := secretStore.Secret("weavepassword")
if weavesecret != nil { if weavesecret != nil {
@ -166,7 +165,7 @@ func (tf *TemplateFunctions) AddTo(dest template.FuncMap, secretStore fi.SecretS
dest["WeaveSecret"] = func() string { return weavesecretString } dest["WeaveSecret"] = func() string { return weavesecretString }
} }
if tf.cluster.Spec.Networking != nil && tf.cluster.Spec.Networking.Cilium != nil { if cluster.Spec.Networking != nil && cluster.Spec.Networking.Cilium != nil {
ciliumsecretString := "" ciliumsecretString := ""
ciliumsecret, _ := secretStore.Secret("ciliumpassword") ciliumsecret, _ := secretStore.Secret("ciliumpassword")
if ciliumsecret != nil { if ciliumsecret != nil {
@ -195,7 +194,7 @@ func (tf *TemplateFunctions) ToJSON(data interface{}) string {
// EtcdScheme parses and grabs the protocol to the etcd cluster // EtcdScheme parses and grabs the protocol to the etcd cluster
func (tf *TemplateFunctions) EtcdScheme() string { func (tf *TemplateFunctions) EtcdScheme() string {
if tf.modelContext.UseEtcdTLS() { if tf.UseEtcdTLS() {
return "https" return "https"
} }
@ -204,7 +203,7 @@ func (tf *TemplateFunctions) EtcdScheme() string {
// SharedVPC is a simple helper function which makes the templates for a shared VPC clearer // SharedVPC is a simple helper function which makes the templates for a shared VPC clearer
func (tf *TemplateFunctions) SharedVPC() bool { func (tf *TemplateFunctions) SharedVPC() bool {
return tf.cluster.SharedVPC() return tf.Cluster.SharedVPC()
} }
// HasTag returns true if the specified tag is set // HasTag returns true if the specified tag is set
@ -215,53 +214,54 @@ func (tf *TemplateFunctions) HasTag(tag string) bool {
// GetInstanceGroup returns the instance group with the specified name // GetInstanceGroup returns the instance group with the specified name
func (tf *TemplateFunctions) GetInstanceGroup(name string) (*kops.InstanceGroup, error) { func (tf *TemplateFunctions) GetInstanceGroup(name string) (*kops.InstanceGroup, error) {
for _, ig := range tf.instanceGroups { ig := tf.KopsModelContext.FindInstanceGroup(name)
if ig.ObjectMeta.Name == name { if ig == nil {
return ig, nil return nil, fmt.Errorf("InstanceGroup %q not found", name)
}
} }
return nil, fmt.Errorf("InstanceGroup %q not found", name) return ig, nil
} }
// CloudControllerConfigArgv returns the args to external cloud controller // CloudControllerConfigArgv returns the args to external cloud controller
func (tf *TemplateFunctions) CloudControllerConfigArgv() ([]string, error) { func (tf *TemplateFunctions) CloudControllerConfigArgv() ([]string, error) {
if tf.cluster.Spec.ExternalCloudControllerManager == nil { cluster := tf.Cluster
if cluster.Spec.ExternalCloudControllerManager == nil {
return nil, fmt.Errorf("ExternalCloudControllerManager is nil") return nil, fmt.Errorf("ExternalCloudControllerManager is nil")
} }
var argv []string var argv []string
if tf.cluster.Spec.ExternalCloudControllerManager.Master != "" { if cluster.Spec.ExternalCloudControllerManager.Master != "" {
argv = append(argv, fmt.Sprintf("--master=%s", tf.cluster.Spec.ExternalCloudControllerManager.Master)) argv = append(argv, fmt.Sprintf("--master=%s", cluster.Spec.ExternalCloudControllerManager.Master))
} }
if tf.cluster.Spec.ExternalCloudControllerManager.LogLevel != 0 { if cluster.Spec.ExternalCloudControllerManager.LogLevel != 0 {
argv = append(argv, fmt.Sprintf("--v=%d", tf.cluster.Spec.ExternalCloudControllerManager.LogLevel)) argv = append(argv, fmt.Sprintf("--v=%d", cluster.Spec.ExternalCloudControllerManager.LogLevel))
} else { } else {
argv = append(argv, "--v=2") argv = append(argv, "--v=2")
} }
if tf.cluster.Spec.ExternalCloudControllerManager.CloudProvider != "" { if cluster.Spec.ExternalCloudControllerManager.CloudProvider != "" {
argv = append(argv, fmt.Sprintf("--cloud-provider=%s", tf.cluster.Spec.ExternalCloudControllerManager.CloudProvider)) argv = append(argv, fmt.Sprintf("--cloud-provider=%s", cluster.Spec.ExternalCloudControllerManager.CloudProvider))
} else if tf.cluster.Spec.CloudProvider != "" { } else if cluster.Spec.CloudProvider != "" {
argv = append(argv, fmt.Sprintf("--cloud-provider=%s", tf.cluster.Spec.CloudProvider)) argv = append(argv, fmt.Sprintf("--cloud-provider=%s", cluster.Spec.CloudProvider))
} else { } else {
return nil, fmt.Errorf("Cloud Provider is not set") return nil, fmt.Errorf("Cloud Provider is not set")
} }
if tf.cluster.Spec.ExternalCloudControllerManager.ClusterName != "" { if cluster.Spec.ExternalCloudControllerManager.ClusterName != "" {
argv = append(argv, fmt.Sprintf("--cluster-name=%s", tf.cluster.Spec.ExternalCloudControllerManager.ClusterName)) argv = append(argv, fmt.Sprintf("--cluster-name=%s", cluster.Spec.ExternalCloudControllerManager.ClusterName))
} }
if tf.cluster.Spec.ExternalCloudControllerManager.ClusterCIDR != "" { if cluster.Spec.ExternalCloudControllerManager.ClusterCIDR != "" {
argv = append(argv, fmt.Sprintf("--cluster-cidr=%s", tf.cluster.Spec.ExternalCloudControllerManager.ClusterCIDR)) argv = append(argv, fmt.Sprintf("--cluster-cidr=%s", cluster.Spec.ExternalCloudControllerManager.ClusterCIDR))
} }
if tf.cluster.Spec.ExternalCloudControllerManager.AllocateNodeCIDRs != nil { if cluster.Spec.ExternalCloudControllerManager.AllocateNodeCIDRs != nil {
argv = append(argv, fmt.Sprintf("--allocate-node-cidrs=%t", *tf.cluster.Spec.ExternalCloudControllerManager.AllocateNodeCIDRs)) argv = append(argv, fmt.Sprintf("--allocate-node-cidrs=%t", *cluster.Spec.ExternalCloudControllerManager.AllocateNodeCIDRs))
} }
if tf.cluster.Spec.ExternalCloudControllerManager.ConfigureCloudRoutes != nil { if cluster.Spec.ExternalCloudControllerManager.ConfigureCloudRoutes != nil {
argv = append(argv, fmt.Sprintf("--configure-cloud-routes=%t", *tf.cluster.Spec.ExternalCloudControllerManager.ConfigureCloudRoutes)) argv = append(argv, fmt.Sprintf("--configure-cloud-routes=%t", *cluster.Spec.ExternalCloudControllerManager.ConfigureCloudRoutes))
} }
if tf.cluster.Spec.ExternalCloudControllerManager.CIDRAllocatorType != nil && *tf.cluster.Spec.ExternalCloudControllerManager.CIDRAllocatorType != "" { if cluster.Spec.ExternalCloudControllerManager.CIDRAllocatorType != nil && *cluster.Spec.ExternalCloudControllerManager.CIDRAllocatorType != "" {
argv = append(argv, fmt.Sprintf("--cidr-allocator-type=%s", *tf.cluster.Spec.ExternalCloudControllerManager.CIDRAllocatorType)) argv = append(argv, fmt.Sprintf("--cidr-allocator-type=%s", *cluster.Spec.ExternalCloudControllerManager.CIDRAllocatorType))
} }
if tf.cluster.Spec.ExternalCloudControllerManager.UseServiceAccountCredentials != nil { if cluster.Spec.ExternalCloudControllerManager.UseServiceAccountCredentials != nil {
argv = append(argv, fmt.Sprintf("--use-service-account-credentials=%t", *tf.cluster.Spec.ExternalCloudControllerManager.UseServiceAccountCredentials)) argv = append(argv, fmt.Sprintf("--use-service-account-credentials=%t", *cluster.Spec.ExternalCloudControllerManager.UseServiceAccountCredentials))
} else { } else {
argv = append(argv, fmt.Sprintf("--use-service-account-credentials=%t", true)) argv = append(argv, fmt.Sprintf("--use-service-account-credentials=%t", true))
} }
@ -271,20 +271,22 @@ func (tf *TemplateFunctions) CloudControllerConfigArgv() ([]string, error) {
// DNSControllerArgv returns the args to the DNS controller // DNSControllerArgv returns the args to the DNS controller
func (tf *TemplateFunctions) DNSControllerArgv() ([]string, error) { func (tf *TemplateFunctions) DNSControllerArgv() ([]string, error) {
cluster := tf.Cluster
var argv []string var argv []string
argv = append(argv, "/usr/bin/dns-controller") argv = append(argv, "/usr/bin/dns-controller")
// @check if the dns controller has custom configuration // @check if the dns controller has custom configuration
if tf.cluster.Spec.ExternalDNS == nil { if cluster.Spec.ExternalDNS == nil {
argv = append(argv, []string{"--watch-ingress=false"}...) argv = append(argv, []string{"--watch-ingress=false"}...)
klog.V(4).Infof("watch-ingress=false set on dns-controller") klog.V(4).Infof("watch-ingress=false set on dns-controller")
} else { } else {
// @check if the watch ingress is set // @check if the watch ingress is set
var watchIngress bool var watchIngress bool
if tf.cluster.Spec.ExternalDNS.WatchIngress != nil { if cluster.Spec.ExternalDNS.WatchIngress != nil {
watchIngress = fi.BoolValue(tf.cluster.Spec.ExternalDNS.WatchIngress) watchIngress = fi.BoolValue(cluster.Spec.ExternalDNS.WatchIngress)
} }
if watchIngress { if watchIngress {
@ -292,45 +294,45 @@ func (tf *TemplateFunctions) DNSControllerArgv() ([]string, error) {
klog.Warningln("this may cause problems with previously defined services: https://github.com/kubernetes/kops/issues/2496") klog.Warningln("this may cause problems with previously defined services: https://github.com/kubernetes/kops/issues/2496")
} }
argv = append(argv, fmt.Sprintf("--watch-ingress=%t", watchIngress)) argv = append(argv, fmt.Sprintf("--watch-ingress=%t", watchIngress))
if tf.cluster.Spec.ExternalDNS.WatchNamespace != "" { if cluster.Spec.ExternalDNS.WatchNamespace != "" {
argv = append(argv, fmt.Sprintf("--watch-namespace=%s", tf.cluster.Spec.ExternalDNS.WatchNamespace)) argv = append(argv, fmt.Sprintf("--watch-namespace=%s", cluster.Spec.ExternalDNS.WatchNamespace))
} }
} }
if dns.IsGossipHostname(tf.cluster.Spec.MasterInternalName) { if dns.IsGossipHostname(cluster.Spec.MasterInternalName) {
argv = append(argv, "--dns=gossip") argv = append(argv, "--dns=gossip")
// Configuration specifically for the DNS controller gossip // Configuration specifically for the DNS controller gossip
if tf.cluster.Spec.DNSControllerGossipConfig != nil { if cluster.Spec.DNSControllerGossipConfig != nil {
if tf.cluster.Spec.DNSControllerGossipConfig.Protocol != nil { if cluster.Spec.DNSControllerGossipConfig.Protocol != nil {
argv = append(argv, "--gossip-protocol="+*tf.cluster.Spec.DNSControllerGossipConfig.Protocol) argv = append(argv, "--gossip-protocol="+*cluster.Spec.DNSControllerGossipConfig.Protocol)
} }
if tf.cluster.Spec.DNSControllerGossipConfig.Listen != nil { if cluster.Spec.DNSControllerGossipConfig.Listen != nil {
argv = append(argv, "--gossip-listen="+*tf.cluster.Spec.DNSControllerGossipConfig.Listen) argv = append(argv, "--gossip-listen="+*cluster.Spec.DNSControllerGossipConfig.Listen)
} }
if tf.cluster.Spec.DNSControllerGossipConfig.Secret != nil { if cluster.Spec.DNSControllerGossipConfig.Secret != nil {
argv = append(argv, "--gossip-secret="+*tf.cluster.Spec.DNSControllerGossipConfig.Secret) argv = append(argv, "--gossip-secret="+*cluster.Spec.DNSControllerGossipConfig.Secret)
} }
if tf.cluster.Spec.DNSControllerGossipConfig.Seed != nil { if cluster.Spec.DNSControllerGossipConfig.Seed != nil {
argv = append(argv, "--gossip-seed="+*tf.cluster.Spec.DNSControllerGossipConfig.Seed) argv = append(argv, "--gossip-seed="+*cluster.Spec.DNSControllerGossipConfig.Seed)
} else { } else {
argv = append(argv, fmt.Sprintf("--gossip-seed=127.0.0.1:%d", wellknownports.ProtokubeGossipWeaveMesh)) argv = append(argv, fmt.Sprintf("--gossip-seed=127.0.0.1:%d", wellknownports.ProtokubeGossipWeaveMesh))
} }
if tf.cluster.Spec.DNSControllerGossipConfig.Secondary != nil { if cluster.Spec.DNSControllerGossipConfig.Secondary != nil {
if tf.cluster.Spec.DNSControllerGossipConfig.Secondary.Protocol != nil { if cluster.Spec.DNSControllerGossipConfig.Secondary.Protocol != nil {
argv = append(argv, "--gossip-protocol-secondary="+*tf.cluster.Spec.DNSControllerGossipConfig.Secondary.Protocol) argv = append(argv, "--gossip-protocol-secondary="+*cluster.Spec.DNSControllerGossipConfig.Secondary.Protocol)
} }
if tf.cluster.Spec.DNSControllerGossipConfig.Secondary.Listen != nil { if cluster.Spec.DNSControllerGossipConfig.Secondary.Listen != nil {
argv = append(argv, "--gossip-listen-secondary="+*tf.cluster.Spec.DNSControllerGossipConfig.Secondary.Listen) argv = append(argv, "--gossip-listen-secondary="+*cluster.Spec.DNSControllerGossipConfig.Secondary.Listen)
} }
if tf.cluster.Spec.DNSControllerGossipConfig.Secondary.Secret != nil { if cluster.Spec.DNSControllerGossipConfig.Secondary.Secret != nil {
argv = append(argv, "--gossip-secret-secondary="+*tf.cluster.Spec.DNSControllerGossipConfig.Secondary.Secret) argv = append(argv, "--gossip-secret-secondary="+*cluster.Spec.DNSControllerGossipConfig.Secondary.Secret)
} }
if tf.cluster.Spec.DNSControllerGossipConfig.Secondary.Seed != nil { if cluster.Spec.DNSControllerGossipConfig.Secondary.Seed != nil {
argv = append(argv, "--gossip-seed-secondary="+*tf.cluster.Spec.DNSControllerGossipConfig.Secondary.Seed) argv = append(argv, "--gossip-seed-secondary="+*cluster.Spec.DNSControllerGossipConfig.Secondary.Seed)
} else { } else {
argv = append(argv, fmt.Sprintf("--gossip-seed-secondary=127.0.0.1:%d", wellknownports.ProtokubeGossipMemberlist)) argv = append(argv, fmt.Sprintf("--gossip-seed-secondary=127.0.0.1:%d", wellknownports.ProtokubeGossipMemberlist))
} }
@ -344,7 +346,7 @@ func (tf *TemplateFunctions) DNSControllerArgv() ([]string, error) {
argv = append(argv, fmt.Sprintf("--gossip-seed-secondary=127.0.0.1:%d", wellknownports.ProtokubeGossipMemberlist)) argv = append(argv, fmt.Sprintf("--gossip-seed-secondary=127.0.0.1:%d", wellknownports.ProtokubeGossipMemberlist))
} }
} else { } else {
switch kops.CloudProviderID(tf.cluster.Spec.CloudProvider) { switch kops.CloudProviderID(cluster.Spec.CloudProvider) {
case kops.CloudProviderAWS: case kops.CloudProviderAWS:
if strings.HasPrefix(os.Getenv("AWS_REGION"), "cn-") { if strings.HasPrefix(os.Getenv("AWS_REGION"), "cn-") {
argv = append(argv, "--dns=gossip") argv = append(argv, "--dns=gossip")
@ -357,11 +359,11 @@ func (tf *TemplateFunctions) DNSControllerArgv() ([]string, error) {
argv = append(argv, "--dns=digitalocean") argv = append(argv, "--dns=digitalocean")
default: default:
return nil, fmt.Errorf("unhandled cloudprovider %q", tf.cluster.Spec.CloudProvider) return nil, fmt.Errorf("unhandled cloudprovider %q", cluster.Spec.CloudProvider)
} }
} }
zone := tf.cluster.Spec.DNSZone zone := cluster.Spec.DNSZone
if zone != "" { if zone != "" {
if strings.Contains(zone, ".") { if strings.Contains(zone, ".") {
// match by name // match by name
@ -381,9 +383,11 @@ func (tf *TemplateFunctions) DNSControllerArgv() ([]string, error) {
// KopsControllerConfig returns the yaml configuration for kops-controller // KopsControllerConfig returns the yaml configuration for kops-controller
func (tf *TemplateFunctions) KopsControllerConfig() (string, error) { func (tf *TemplateFunctions) KopsControllerConfig() (string, error) {
cluster := tf.Cluster
config := &kopscontrollerconfig.Options{ config := &kopscontrollerconfig.Options{
Cloud: tf.cluster.Spec.CloudProvider, Cloud: cluster.Spec.CloudProvider,
ConfigBase: tf.cluster.Spec.ConfigBase, ConfigBase: cluster.Spec.ConfigBase,
} }
// To avoid indentation problems, we marshal as json. json is a subset of yaml // To avoid indentation problems, we marshal as json. json is a subset of yaml
@ -397,7 +401,6 @@ func (tf *TemplateFunctions) KopsControllerConfig() (string, error) {
// KopsControllerArgv returns the args to kops-controller // KopsControllerArgv returns the args to kops-controller
func (tf *TemplateFunctions) KopsControllerArgv() ([]string, error) { func (tf *TemplateFunctions) KopsControllerArgv() ([]string, error) {
var argv []string var argv []string
argv = append(argv, "/usr/bin/kops-controller") argv = append(argv, "/usr/bin/kops-controller")
@ -411,19 +414,21 @@ func (tf *TemplateFunctions) KopsControllerArgv() ([]string, error) {
} }
func (tf *TemplateFunctions) ExternalDNSArgv() ([]string, error) { func (tf *TemplateFunctions) ExternalDNSArgv() ([]string, error) {
cluster := tf.Cluster
var argv []string var argv []string
cloudProvider := tf.cluster.Spec.CloudProvider cloudProvider := cluster.Spec.CloudProvider
switch kops.CloudProviderID(cloudProvider) { switch kops.CloudProviderID(cloudProvider) {
case kops.CloudProviderAWS: case kops.CloudProviderAWS:
argv = append(argv, "--provider=aws") argv = append(argv, "--provider=aws")
case kops.CloudProviderGCE: case kops.CloudProviderGCE:
project := tf.cluster.Spec.Project project := cluster.Spec.Project
argv = append(argv, "--provider=google") argv = append(argv, "--provider=google")
argv = append(argv, "--google-project="+project) argv = append(argv, "--google-project="+project)
default: default:
return nil, fmt.Errorf("unhandled cloudprovider %q", tf.cluster.Spec.CloudProvider) return nil, fmt.Errorf("unhandled cloudprovider %q", cluster.Spec.CloudProvider)
} }
argv = append(argv, "--source=ingress") argv = append(argv, "--source=ingress")
@ -432,8 +437,10 @@ func (tf *TemplateFunctions) ExternalDNSArgv() ([]string, error) {
} }
func (tf *TemplateFunctions) ProxyEnv() map[string]string { func (tf *TemplateFunctions) ProxyEnv() map[string]string {
cluster := tf.Cluster
envs := map[string]string{} envs := map[string]string{}
proxies := tf.cluster.Spec.EgressProxy proxies := cluster.Spec.EgressProxy
if proxies == nil { if proxies == nil {
return envs return envs
} }
@ -458,7 +465,7 @@ func (tf *TemplateFunctions) ProxyEnv() map[string]string {
// KopsSystemEnv builds the env vars for a system component // KopsSystemEnv builds the env vars for a system component
func (tf *TemplateFunctions) KopsSystemEnv() []corev1.EnvVar { func (tf *TemplateFunctions) KopsSystemEnv() []corev1.EnvVar {
envMap := env.BuildSystemComponentEnvVars(&tf.cluster.Spec) envMap := env.BuildSystemComponentEnvVars(&tf.Cluster.Spec)
return envMap.ToEnvVars() return envMap.ToEnvVars()
} }
@ -467,7 +474,7 @@ func (tf *TemplateFunctions) KopsSystemEnv() []corev1.EnvVar {
// with tag specified to k8s version // with tag specified to k8s version
func (tf *TemplateFunctions) OpenStackCCM() string { func (tf *TemplateFunctions) OpenStackCCM() string {
var tag string var tag string
parsed, err := util.ParseKubernetesVersion(tf.cluster.Spec.KubernetesVersion) parsed, err := util.ParseKubernetesVersion(tf.Cluster.Spec.KubernetesVersion)
if err != nil { if err != nil {
tag = "latest" tag = "latest"
} else { } else {

View File

@ -200,9 +200,9 @@ func Test_TemplateFunctions_CloudControllerConfigArgv(t *testing.T) {
} }
for _, testCase := range tests { for _, testCase := range tests {
t.Run(testCase.desc, func(t *testing.T) { t.Run(testCase.desc, func(t *testing.T) {
tf := &TemplateFunctions{ tf := &TemplateFunctions{}
cluster: testCase.cluster, tf.Cluster = testCase.cluster
}
actual, error := tf.CloudControllerConfigArgv() actual, error := tf.CloudControllerConfigArgv()
if !reflect.DeepEqual(error, testCase.expectedError) { if !reflect.DeepEqual(error, testCase.expectedError) {
t.Errorf("Error differs: %+v instead of %+v", error, testCase.expectedError) t.Errorf("Error differs: %+v instead of %+v", error, testCase.expectedError)
@ -260,7 +260,8 @@ func Test_executeTemplate(t *testing.T) {
tpl := template.New(filepath.Base(templateFileAbsolutePath)) tpl := template.New(filepath.Base(templateFileAbsolutePath))
funcMap := make(template.FuncMap) funcMap := make(template.FuncMap)
templateFunctions := TemplateFunctions{cluster: testCase.cluster} templateFunctions := TemplateFunctions{}
templateFunctions.Cluster = testCase.cluster
templateFunctions.AddTo(funcMap, nil) templateFunctions.AddTo(funcMap, nil)
tpl.Funcs(funcMap) tpl.Funcs(funcMap)