Remove some rather long networking nil checks

This commit is contained in:
Ole Markus With 2020-05-21 10:16:22 +02:00
parent 95d2170fa6
commit d1ff25bb4e
10 changed files with 41 additions and 129 deletions

View File

@ -1109,7 +1109,7 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
case api.TopologyPrivate: case api.TopologyPrivate:
if !supportsPrivateTopology(cluster.Spec.Networking) { if !supportsPrivateTopology(cluster.Spec.Networking) {
return fmt.Errorf("Invalid networking option %s. Currently only '--networking kopeio-vxlan (or kopeio)', '--networking weave', '--networking flannel', '--networking calico', '--networking canal', '--networking kube-router', '--networking romana', '--networking amazon-vpc-routed-eni', '--networking cilium', '--networking lyftvpc', '--networking cni' are supported for private topologies", c.Networking) return fmt.Errorf("invalid networking option %s. Kubenet does not support private topology", c.Networking)
} }
cluster.Spec.Topology = &api.TopologySpec{ cluster.Spec.Topology = &api.TopologySpec{
Masters: api.TopologyPrivate, Masters: api.TopologyPrivate,
@ -1452,11 +1452,7 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
} }
func supportsPrivateTopology(n *api.NetworkingSpec) bool { func supportsPrivateTopology(n *api.NetworkingSpec) bool {
return n.Kubenet == nil
if n.CNI != nil || n.Kopeio != nil || n.Weave != nil || n.Flannel != nil || n.Calico != nil || n.Canal != nil || n.Kuberouter != nil || n.Romana != nil || n.AmazonVPC != nil || n.Cilium != nil || n.LyftVPC != nil || n.GCE != nil {
return true
}
return false
} }
func trimCommonPrefix(names []string) []string { func trimCommonPrefix(names []string) []string {

View File

@ -343,16 +343,6 @@ func (c *NodeupModelContext) UseEtcdTLSAuth() bool {
return false return false
} }
// UsesCNI checks if the cluster has CNI configured
func (c *NodeupModelContext) UsesCNI() bool {
networking := c.Cluster.Spec.Networking
if networking == nil || networking.Classic != nil {
return false
}
return true
}
// UseNodeAuthorization checks if have a node authorization policy // UseNodeAuthorization checks if have a node authorization policy
func (c *NodeupModelContext) UseNodeAuthorization() bool { func (c *NodeupModelContext) UseNodeAuthorization() bool {
return c.Cluster.Spec.NodeAuthorization != nil return c.Cluster.Spec.NodeAuthorization != nil

View File

@ -25,6 +25,8 @@ import (
"strings" "strings"
"time" "time"
"k8s.io/kops/pkg/model/components"
"github.com/aws/aws-sdk-go/aws/ec2metadata" "github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
@ -134,7 +136,7 @@ func (b *KubeletBuilder) Build(c *fi.ModelBuilderContext) error {
} }
} }
if b.UsesCNI() { if components.UsesCNI(b.Cluster.Spec.Networking) {
c.AddTask(&nodetasks.File{ c.AddTask(&nodetasks.File{
Path: b.CNIConfDir(), Path: b.CNIConfDir(),
Type: nodetasks.FileType_Directory, Type: nodetasks.FileType_Directory,
@ -212,10 +214,8 @@ func (b *KubeletBuilder) buildSystemdEnvironmentFile(kubeletConfig *kops.Kubelet
flags += " --cloud-config=" + CloudConfigFilePath flags += " --cloud-config=" + CloudConfigFilePath
} }
if b.UsesCNI() { flags += " --cni-bin-dir=" + b.CNIBinDir()
flags += " --cni-bin-dir=" + b.CNIBinDir() flags += " --cni-conf-dir=" + b.CNIConfDir()
flags += " --cni-conf-dir=" + b.CNIConfDir()
}
if b.UsesSecondaryIP() { if b.UsesSecondaryIP() {
sess := session.Must(session.NewSession()) sess := session.Must(session.NewSession())

View File

@ -76,26 +76,31 @@ func KubernetesVersion(clusterSpec *kops.ClusterSpec) (*semver.Version, error) {
} }
// UsesKubenet returns true if our networking is derived from kubenet // UsesKubenet returns true if our networking is derived from kubenet
func UsesKubenet(clusterSpec *kops.ClusterSpec) (bool, error) { func UsesKubenet(networking *kops.NetworkingSpec) bool {
networking := clusterSpec.Networking if networking == nil {
if networking == nil || networking.Classic != nil { panic("no networking mode set")
return false, nil }
} else if networking.Kubenet != nil { if networking.Kubenet != nil {
return true, nil return true
} else if networking.GCE != nil { } else if networking.GCE != nil {
// GCE IP Alias networking is based on kubenet // GCE IP Alias networking is based on kubenet
return true, nil return true
} else if networking.External != nil { } else if networking.External != nil {
// external is based on kubenet // external is based on kubenet
return true, nil return true
} else if networking.CNI != nil || networking.Weave != nil || networking.Flannel != nil || networking.Calico != nil || networking.Canal != nil || networking.Kuberouter != nil || networking.Romana != nil || networking.AmazonVPC != nil || networking.Cilium != nil || networking.LyftVPC != nil {
return false, nil
} else if networking.Kopeio != nil { } else if networking.Kopeio != nil {
// Kopeio is based on kubenet / external // Kopeio is based on kubenet / external
return true, nil return true
} else {
return false, fmt.Errorf("no networking mode set")
} }
return false
}
// UsesCNI returns true if the networking provider is a CNI plugin
func UsesCNI(networking *kops.NetworkingSpec) bool {
// Kubenet and CNI are the only kubelet networking plugins right now.
return !UsesKubenet(networking)
} }
func WellKnownServiceIP(clusterSpec *kops.ClusterSpec, id int) (net.IP, error) { func WellKnownServiceIP(clusterSpec *kops.ClusterSpec, id int) (net.IP, error) {

View File

@ -20,6 +20,8 @@ import (
"fmt" "fmt"
"time" "time"
"k8s.io/kops/pkg/model/components"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog" "k8s.io/klog"
"k8s.io/kops/pkg/apis/kops" "k8s.io/kops/pkg/apis/kops"
@ -140,7 +142,7 @@ func (b *KubeControllerManagerOptionsBuilder) BuildOptions(o interface{}) error
} }
} else if networking.External != nil { } else if networking.External != nil {
kcm.ConfigureCloudRoutes = fi.Bool(false) kcm.ConfigureCloudRoutes = fi.Bool(false)
} else if networking.CNI != nil || networking.Weave != nil || networking.Flannel != nil || networking.Calico != nil || networking.Canal != nil || networking.Kuberouter != nil || networking.Romana != nil || networking.AmazonVPC != nil || networking.Cilium != nil || networking.LyftVPC != nil { } else if components.UsesCNI(networking) {
kcm.ConfigureCloudRoutes = fi.Bool(false) kcm.ConfigureCloudRoutes = fi.Bool(false)
} else if networking.Kopeio != nil { } else if networking.Kopeio != nil {
// Kopeio is based on kubenet / external // Kopeio is based on kubenet / external

View File

@ -17,6 +17,7 @@ limitations under the License.
package components package components
import ( import (
"fmt"
"strings" "strings"
"k8s.io/klog" "k8s.io/klog"
@ -173,11 +174,12 @@ func (b *KubeletOptionsBuilder) BuildOptions(o interface{}) error {
clusterSpec.Kubelet.CloudProvider = "external" clusterSpec.Kubelet.CloudProvider = "external"
} }
usesKubenet, err := UsesKubenet(clusterSpec) networking := clusterSpec.Networking
if err != nil { if networking == nil {
return err return fmt.Errorf("no networking mode set")
} }
if usesKubenet { if UsesKubenet(networking) {
clusterSpec.Kubelet.NetworkPluginName = "kubenet" clusterSpec.Kubelet.NetworkPluginName = "kubenet"
// AWS MTU is 9001 // AWS MTU is 9001

View File

@ -29,6 +29,7 @@ func buildKubeletTestCluster() *kops.Cluster {
KubernetesVersion: "1.6.2", KubernetesVersion: "1.6.2",
ServiceClusterIPRange: "10.10.0.0/16", ServiceClusterIPRange: "10.10.0.0/16",
Kubelet: &kops.KubeletConfigSpec{}, Kubelet: &kops.KubeletConfigSpec{},
Networking: &kops.NetworkingSpec{},
}, },
} }
} }

View File

@ -43,7 +43,7 @@ func (b *NetworkingOptionsBuilder) BuildOptions(o interface{}) error {
return fmt.Errorf("networking not set") return fmt.Errorf("networking not set")
} }
if networking.CNI != nil || networking.Weave != nil || networking.Flannel != nil || networking.Calico != nil || networking.Canal != nil || networking.Kuberouter != nil || networking.Romana != nil || networking.AmazonVPC != nil || networking.Cilium != nil || networking.LyftVPC != nil { if UsesCNI(networking) {
options.Kubelet.NetworkPluginName = "cni" options.Kubelet.NetworkPluginName = "cni"
// ConfigureCBR0 flag removed from 1.5 // ConfigureCBR0 flag removed from 1.5

View File

@ -1200,15 +1200,13 @@ func (c *ApplyClusterCmd) AddFileAssets(assetBuilder *assets.AssetBuilder) error
c.Assets = append(c.Assets, BuildMirroredAsset(u, hash)) c.Assets = append(c.Assets, BuildMirroredAsset(u, hash))
} }
if usesCNI(c.Cluster) { cniAsset, cniAssetHash, err := findCNIAssets(c.Cluster, assetBuilder)
cniAsset, cniAssetHash, err := findCNIAssets(c.Cluster, assetBuilder) if err != nil {
if err != nil { return err
return err
}
c.Assets = append(c.Assets, BuildMirroredAsset(cniAsset, cniAssetHash))
} }
c.Assets = append(c.Assets, BuildMirroredAsset(cniAsset, cniAssetHash))
if c.Cluster.Spec.Networking.LyftVPC != nil { if c.Cluster.Spec.Networking.LyftVPC != nil {
var hash *hashing.Hash var hash *hashing.Hash

View File

@ -28,88 +28,6 @@ import (
"k8s.io/kops/util/pkg/hashing" "k8s.io/kops/util/pkg/hashing"
) )
func usesCNI(c *kopsapi.Cluster) bool {
networkConfig := c.Spec.Networking
if networkConfig == nil || networkConfig.Classic != nil {
// classic
return false
}
if networkConfig.Kubenet != nil {
// kubenet is now configured via CNI
return true
}
if networkConfig.GCE != nil {
// GCE is kubenet at the node level
return true
}
if networkConfig.External != nil {
// external: assume uses CNI
return true
}
if networkConfig.Kopeio != nil {
// Kopeio uses kubenet (and thus CNI)
return true
}
if networkConfig.Weave != nil {
// Weave uses CNI
return true
}
if networkConfig.Flannel != nil {
// Flannel uses CNI
return true
}
if networkConfig.Calico != nil {
// Calico uses CNI
return true
}
if networkConfig.Canal != nil {
// Canal uses CNI
return true
}
if networkConfig.Kuberouter != nil {
// Kuberouter uses CNI
return true
}
if networkConfig.Romana != nil {
// Romana uses CNI
return true
}
if networkConfig.AmazonVPC != nil {
// AmazonVPC uses CNI
return true
}
if networkConfig.Cilium != nil {
// Cilium uses CNI
return true
}
if networkConfig.CNI != nil {
// CNI definitely uses CNI!
return true
}
if networkConfig.LyftVPC != nil {
// LyftVPC uses CNI
return true
}
// Assume other modes also use CNI
klog.Warningf("Unknown networking mode configured")
return true
}
// TODO: we really need to sort this out: // TODO: we really need to sort this out:
// https://github.com/kubernetes/kops/issues/724 // https://github.com/kubernetes/kops/issues/724
// https://github.com/kubernetes/kops/issues/626 // https://github.com/kubernetes/kops/issues/626