Use same method receiver names everywhere

This commit is contained in:
Ciprian Hacman 2021-04-30 11:31:14 +03:00
parent bd7176f45f
commit c49b71feb5
2 changed files with 71 additions and 71 deletions

View File

@ -51,14 +51,14 @@ type KopsModelContext struct {
}
// GatherSubnets maps the subnet names in an InstanceGroup to the ClusterSubnetSpec objects (which are stored on the Cluster)
func (m *KopsModelContext) GatherSubnets(ig *kops.InstanceGroup) ([]*kops.ClusterSubnetSpec, error) {
func (b *KopsModelContext) GatherSubnets(ig *kops.InstanceGroup) ([]*kops.ClusterSubnetSpec, error) {
var subnets []*kops.ClusterSubnetSpec
var subnetType kops.SubnetType
for _, subnetName := range ig.Spec.Subnets {
var matches []*kops.ClusterSubnetSpec
for i := range m.Cluster.Spec.Subnets {
clusterSubnet := &m.Cluster.Spec.Subnets[i]
for i := range b.Cluster.Spec.Subnets {
clusterSubnet := &b.Cluster.Spec.Subnets[i]
if clusterSubnet.Name == subnetName {
matches = append(matches, clusterSubnet)
}
@ -86,8 +86,8 @@ func (m *KopsModelContext) GatherSubnets(ig *kops.InstanceGroup) ([]*kops.Cluste
}
// FindInstanceGroup returns the instance group with the matching Name (or nil if not found)
func (m *KopsModelContext) FindInstanceGroup(name string) *kops.InstanceGroup {
for _, ig := range m.InstanceGroups {
func (b *KopsModelContext) FindInstanceGroup(name string) *kops.InstanceGroup {
for _, ig := range b.InstanceGroups {
if ig.ObjectMeta.Name == name {
return ig
}
@ -96,19 +96,19 @@ func (m *KopsModelContext) FindInstanceGroup(name string) *kops.InstanceGroup {
}
// FindSubnet returns the subnet with the matching Name (or nil if not found)
func (m *KopsModelContext) FindSubnet(name string) *kops.ClusterSubnetSpec {
return model.FindSubnet(m.Cluster, name)
func (b *KopsModelContext) FindSubnet(name string) *kops.ClusterSubnetSpec {
return model.FindSubnet(b.Cluster, name)
}
// FindZonesForInstanceGroup finds the zones for an InstanceGroup
func (m *KopsModelContext) FindZonesForInstanceGroup(ig *kops.InstanceGroup) ([]string, error) {
return model.FindZonesForInstanceGroup(m.Cluster, ig)
func (b *KopsModelContext) FindZonesForInstanceGroup(ig *kops.InstanceGroup) ([]string, error) {
return model.FindZonesForInstanceGroup(b.Cluster, ig)
}
// MasterInstanceGroups returns InstanceGroups with the master role
func (m *KopsModelContext) MasterInstanceGroups() []*kops.InstanceGroup {
func (b *KopsModelContext) MasterInstanceGroups() []*kops.InstanceGroup {
var groups []*kops.InstanceGroup
for _, ig := range m.InstanceGroups {
for _, ig := range b.InstanceGroups {
if !ig.IsMaster() {
continue
}
@ -118,9 +118,9 @@ func (m *KopsModelContext) MasterInstanceGroups() []*kops.InstanceGroup {
}
// NodeInstanceGroups returns InstanceGroups with the node role
func (m *KopsModelContext) NodeInstanceGroups() []*kops.InstanceGroup {
func (b *KopsModelContext) NodeInstanceGroups() []*kops.InstanceGroup {
var groups []*kops.InstanceGroup
for _, ig := range m.InstanceGroups {
for _, ig := range b.InstanceGroups {
if ig.Spec.Role != kops.InstanceGroupRoleNode {
continue
}
@ -130,11 +130,11 @@ func (m *KopsModelContext) NodeInstanceGroups() []*kops.InstanceGroup {
}
// CloudTagsForInstanceGroup computes the tags to apply to instances in the specified InstanceGroup
func (m *KopsModelContext) CloudTagsForInstanceGroup(ig *kops.InstanceGroup) (map[string]string, error) {
labels := m.CloudTags(m.AutoscalingGroupName(ig), false)
func (b *KopsModelContext) CloudTagsForInstanceGroup(ig *kops.InstanceGroup) (map[string]string, error) {
labels := b.CloudTags(b.AutoscalingGroupName(ig), false)
// Apply any user-specified global labels first so they can be overridden by IG-specific labels
for k, v := range m.Cluster.Spec.CloudLabels {
for k, v := range b.Cluster.Spec.CloudLabels {
labels[k] = v
}
@ -144,13 +144,13 @@ func (m *KopsModelContext) CloudTagsForInstanceGroup(ig *kops.InstanceGroup) (ma
}
// Apply NTH Labels
nth := m.Cluster.Spec.NodeTerminationHandler
nth := b.Cluster.Spec.NodeTerminationHandler
if nth != nil && fi.BoolValue(nth.Enabled) && fi.BoolValue(nth.EnableSQSTerminationDraining) {
labels[fi.StringValue(nth.ManagedASGTag)] = ""
}
// Apply labels for cluster autoscaler node labels
for k, v := range nodelabels.BuildNodeLabels(m.Cluster, ig) {
for k, v := range nodelabels.BuildNodeLabels(b.Cluster, ig) {
labels[nodeidentityaws.ClusterAutoscalerNodeTemplateLabel+k] = v
}
@ -186,10 +186,10 @@ func (m *KopsModelContext) CloudTagsForInstanceGroup(ig *kops.InstanceGroup) (ma
}
// CloudTags computes the tags to apply to a normal cloud resource with the specified name
func (m *KopsModelContext) CloudTags(name string, shared bool) map[string]string {
func (b *KopsModelContext) CloudTags(name string, shared bool) map[string]string {
tags := make(map[string]string)
switch kops.CloudProviderID(m.Cluster.Spec.CloudProvider) {
switch kops.CloudProviderID(b.Cluster.Spec.CloudProvider) {
case kops.CloudProviderAWS:
if shared {
// If the resource is shared, we don't try to set the Name - we presume that is managed externally
@ -209,14 +209,14 @@ func (m *KopsModelContext) CloudTags(name string, shared bool) map[string]string
setLegacyTag = false
}
if setLegacyTag {
tags[awsup.TagClusterName] = m.Cluster.ObjectMeta.Name
tags[awsup.TagClusterName] = b.Cluster.ObjectMeta.Name
}
if shared {
tags["kubernetes.io/cluster/"+m.Cluster.ObjectMeta.Name] = "shared"
tags["kubernetes.io/cluster/"+b.Cluster.ObjectMeta.Name] = "shared"
} else {
tags["kubernetes.io/cluster/"+m.Cluster.ObjectMeta.Name] = "owned"
for k, v := range m.Cluster.Spec.CloudLabels {
tags["kubernetes.io/cluster/"+b.Cluster.ObjectMeta.Name] = "owned"
for k, v := range b.Cluster.Spec.CloudLabels {
tags[k] = v
}
}
@ -226,30 +226,30 @@ func (m *KopsModelContext) CloudTags(name string, shared bool) map[string]string
}
// UseKopsControllerForNodeBootstrap checks if nodeup should use kops-controller to bootstrap.
func (m *KopsModelContext) UseKopsControllerForNodeBootstrap() bool {
return model.UseKopsControllerForNodeBootstrap(m.Cluster)
func (b *KopsModelContext) UseKopsControllerForNodeBootstrap() bool {
return model.UseKopsControllerForNodeBootstrap(b.Cluster)
}
// UseBootstrapTokens checks if bootstrap tokens are enabled
func (m *KopsModelContext) UseBootstrapTokens() bool {
if m.Cluster.Spec.KubeAPIServer == nil || m.UseKopsControllerForNodeBootstrap() {
func (b *KopsModelContext) UseBootstrapTokens() bool {
if b.Cluster.Spec.KubeAPIServer == nil || b.UseKopsControllerForNodeBootstrap() {
return false
}
return fi.BoolValue(m.Cluster.Spec.KubeAPIServer.EnableBootstrapAuthToken)
return fi.BoolValue(b.Cluster.Spec.KubeAPIServer.EnableBootstrapAuthToken)
}
// UsesBastionDns checks if we should use a specific name for the bastion dns
func (m *KopsModelContext) UsesBastionDns() bool {
if m.Cluster.Spec.Topology.Bastion != nil && m.Cluster.Spec.Topology.Bastion.BastionPublicName != "" {
func (b *KopsModelContext) UsesBastionDns() bool {
if b.Cluster.Spec.Topology.Bastion != nil && b.Cluster.Spec.Topology.Bastion.BastionPublicName != "" {
return true
}
return false
}
// UsesSSHBastion checks if we have a Bastion in the cluster
func (m *KopsModelContext) UsesSSHBastion() bool {
for _, ig := range m.InstanceGroups {
func (b *KopsModelContext) UsesSSHBastion() bool {
for _, ig := range b.InstanceGroups {
if ig.Spec.Role == kops.InstanceGroupRoleBastion {
return true
}
@ -259,32 +259,32 @@ func (m *KopsModelContext) UsesSSHBastion() bool {
}
// UseLoadBalancerForAPI checks if we are using a load balancer for the kubeapi
func (m *KopsModelContext) UseLoadBalancerForAPI() bool {
if m.Cluster.Spec.API == nil {
func (b *KopsModelContext) UseLoadBalancerForAPI() bool {
if b.Cluster.Spec.API == nil {
return false
}
return m.Cluster.Spec.API.LoadBalancer != nil
return b.Cluster.Spec.API.LoadBalancer != nil
}
// UseLoadBalancerForInternalAPI check if true then we will use the created loadbalancer for internal kubelet
// connections. The intention here is to make connections to apiserver more
// HA - see https://github.com/kubernetes/kops/issues/4252
func (m *KopsModelContext) UseLoadBalancerForInternalAPI() bool {
return m.UseLoadBalancerForAPI() &&
m.Cluster.Spec.API.LoadBalancer.UseForInternalApi
func (b *KopsModelContext) UseLoadBalancerForInternalAPI() bool {
return b.UseLoadBalancerForAPI() &&
b.Cluster.Spec.API.LoadBalancer.UseForInternalApi
}
// APILoadBalancerClass returns which type of load balancer to use for the api
func (m *KopsModelContext) APILoadBalancerClass() kops.LoadBalancerClass {
if m.Cluster.Spec.API != nil && m.Cluster.Spec.API.LoadBalancer != nil {
return m.Cluster.Spec.API.LoadBalancer.Class
func (b *KopsModelContext) APILoadBalancerClass() kops.LoadBalancerClass {
if b.Cluster.Spec.API != nil && b.Cluster.Spec.API.LoadBalancer != nil {
return b.Cluster.Spec.API.LoadBalancer.Class
}
return kops.LoadBalancerClassClassic
}
// UsePrivateDNS checks if we are using private DNS
func (m *KopsModelContext) UsePrivateDNS() bool {
topology := m.Cluster.Spec.Topology
func (b *KopsModelContext) UsePrivateDNS() bool {
topology := b.Cluster.Spec.Topology
if topology != nil && topology.DNS != nil {
switch topology.DNS.Type {
case kops.DNSTypePublic:
@ -302,18 +302,18 @@ func (m *KopsModelContext) UsePrivateDNS() bool {
}
// UseClassicLoadBalancer checks if we are using Classic LoadBalancer
func (m *KopsModelContext) UseClassicLoadBalancer() bool {
return m.Cluster.Spec.API.LoadBalancer.Class == kops.LoadBalancerClassClassic
func (b *KopsModelContext) UseClassicLoadBalancer() bool {
return b.Cluster.Spec.API.LoadBalancer.Class == kops.LoadBalancerClassClassic
}
// UseNetworkLoadBalancer checks if we are using Network LoadBalancer
func (m *KopsModelContext) UseNetworkLoadBalancer() bool {
return m.Cluster.Spec.API.LoadBalancer.Class == kops.LoadBalancerClassNetwork
func (b *KopsModelContext) UseNetworkLoadBalancer() bool {
return b.Cluster.Spec.API.LoadBalancer.Class == kops.LoadBalancerClassNetwork
}
// UseEtcdManager checks to see if etcd manager is enabled
func (c *KopsModelContext) UseEtcdManager() bool {
for _, x := range c.Cluster.Spec.EtcdClusters {
func (b *KopsModelContext) UseEtcdManager() bool {
for _, x := range b.Cluster.Spec.EtcdClusters {
if x.Provider == kops.EtcdProviderTypeManager {
return true
}
@ -323,8 +323,8 @@ func (c *KopsModelContext) UseEtcdManager() bool {
}
// UseEtcdTLS checks to see if etcd tls is enabled
func (m *KopsModelContext) UseEtcdTLS() bool {
for _, x := range m.Cluster.Spec.EtcdClusters {
func (b *KopsModelContext) UseEtcdTLS() bool {
for _, x := range b.Cluster.Spec.EtcdClusters {
if x.EnableEtcdTLS {
return true
}
@ -335,16 +335,16 @@ func (m *KopsModelContext) UseEtcdTLS() bool {
// UseSSHKey returns true if SSHKeyName from the cluster spec is not set to an empty string (""). Setting SSHKeyName
// to an empty string indicates that an SSH key should not be set on instances.
func (m *KopsModelContext) UseSSHKey() bool {
sshKeyName := m.Cluster.Spec.SSHKeyName
func (b *KopsModelContext) UseSSHKey() bool {
sshKeyName := b.Cluster.Spec.SSHKeyName
return sshKeyName == nil || *sshKeyName != ""
}
// KubernetesVersion parses the semver version of kubernetes, from the cluster spec
func (m *KopsModelContext) KubernetesVersion() semver.Version {
func (b *KopsModelContext) KubernetesVersion() semver.Version {
// TODO: Remove copy-pasting c.f. https://github.com/kubernetes/kops/blob/master/pkg/model/components/context.go#L32
kubernetesVersion := m.Cluster.Spec.KubernetesVersion
kubernetesVersion := b.Cluster.Spec.KubernetesVersion
if kubernetesVersion == "" {
klog.Fatalf("KubernetesVersion is required")
@ -358,26 +358,26 @@ func (m *KopsModelContext) KubernetesVersion() semver.Version {
}
// IsKubernetesGTE checks if the kubernetes version is at least version, ignoring prereleases / patches
func (m *KopsModelContext) IsKubernetesGTE(version string) bool {
return util.IsKubernetesGTE(version, m.KubernetesVersion())
func (b *KopsModelContext) IsKubernetesGTE(version string) bool {
return util.IsKubernetesGTE(version, b.KubernetesVersion())
}
// IsKubernetesLT checks if the kubernetes version is before the specified version, ignoring prereleases / patches
func (m *KopsModelContext) IsKubernetesLT(version string) bool {
return !m.IsKubernetesGTE(version)
func (b *KopsModelContext) IsKubernetesLT(version string) bool {
return !b.IsKubernetesGTE(version)
}
// WellKnownServiceIP returns a service ip with the service cidr
func (m *KopsModelContext) WellKnownServiceIP(id int) (net.IP, error) {
return components.WellKnownServiceIP(&m.Cluster.Spec, id)
func (b *KopsModelContext) WellKnownServiceIP(id int) (net.IP, error) {
return components.WellKnownServiceIP(&b.Cluster.Spec, id)
}
// NodePortRange returns the range of ports allocated to NodePorts
func (m *KopsModelContext) NodePortRange() (utilnet.PortRange, error) {
func (b *KopsModelContext) NodePortRange() (utilnet.PortRange, error) {
// defaultServiceNodePortRange is the default port range for NodePort services.
defaultServiceNodePortRange := utilnet.PortRange{Base: 30000, Size: 2768}
kubeApiServer := m.Cluster.Spec.KubeAPIServer
kubeApiServer := b.Cluster.Spec.KubeAPIServer
if kubeApiServer != nil && kubeApiServer.ServiceNodePortRange != "" {
err := defaultServiceNodePortRange.Set(kubeApiServer.ServiceNodePortRange)
if err != nil {
@ -389,6 +389,6 @@ func (m *KopsModelContext) NodePortRange() (utilnet.PortRange, error) {
}
// UseServiceAccountIAM returns true if we are using service-account bound IAM roles.
func (m *KopsModelContext) UseServiceAccountIAM() bool {
func (b *KopsModelContext) UseServiceAccountIAM() bool {
return featureflag.UseServiceAccountIAM.Enabled()
}

View File

@ -86,8 +86,8 @@ func (b *KopsModelContext) LinkToELBSecurityGroup(prefix string) *awstasks.Secur
// LBName32 will attempt to calculate a meaningful name for an ELB given a prefix
// Will never return a string longer than 32 chars
// Note this is _not_ the primary identifier for the ELB - we use the Name tag for that.
func (m *KopsModelContext) LBName32(prefix string) string {
return awsup.GetResourceName32(m.Cluster.ObjectMeta.Name, prefix)
func (b *KopsModelContext) LBName32(prefix string) string {
return awsup.GetResourceName32(b.Cluster.ObjectMeta.Name, prefix)
}
// CLBName returns CLB name plus cluster name
@ -177,19 +177,19 @@ func (b *KopsModelContext) LinkToIAMInstanceProfile(ig *kops.InstanceGroup) (*aw
// SSHKeyName computes a unique SSH key name, combining the cluster name and the SSH public key fingerprint.
// If an SSH key name is provided in the cluster configuration, it will use that instead.
func (c *KopsModelContext) SSHKeyName() (string, error) {
func (b *KopsModelContext) SSHKeyName() (string, error) {
// use configured SSH key name if present
sshKeyName := c.Cluster.Spec.SSHKeyName
sshKeyName := b.Cluster.Spec.SSHKeyName
if sshKeyName != nil && *sshKeyName != "" {
return *sshKeyName, nil
}
fingerprint, err := pki.ComputeOpenSSHKeyFingerprint(string(c.SSHPublicKeys[0]))
fingerprint, err := pki.ComputeOpenSSHKeyFingerprint(string(b.SSHPublicKeys[0]))
if err != nil {
return "", err
}
name := "kubernetes." + c.Cluster.ObjectMeta.Name + "-" + fingerprint
name := "kubernetes." + b.Cluster.ObjectMeta.Name + "-" + fingerprint
return name, nil
}