Skip to content

Commit

Permalink
Apply endpointslice crd when kubernetes version < 1.17
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielXLee committed Jul 16, 2021
1 parent 8734a21 commit e708040
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

generate-embeddedyamls:
go generate controllers/ensures/operator/common/embeddedyamls/generate.go
go generate controllers/embeddedyamls/generate.go

fmt: ## Run go fmt against code.
go fmt ./...
Expand Down
33 changes: 33 additions & 0 deletions controllers/checker/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package checker

import (
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/utils"
)

func CreateOrUpdateEndpointslicesCRD(c client.Client) error {
if err := utils.CreateOrUpdateEmbeddedCRD(c, embeddedyamls.Manifests_fix_crds_discovery_k8s_io_endpointslices_yaml); err != nil {
klog.Errorf("Error creating the EndpointSlice CRD: %v", err)
return err
}
return nil
}
41 changes: 25 additions & 16 deletions controllers/checker/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,40 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package version
package checker

import (
"fmt"
"strconv"
"strings"

"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
)

const (
minK8sMajor = 1 // We need K8s 1.15 for endpoint slices
minK8sMinor = 15
minK8sMajor = 1 // We need K8s 1.15 for endpoint slices
minK8sMinor = 15 // Need patch if k8s minor version < 17 and >= 15
goodK8sMinor = 17 // Don't need patch if k8s minor version >= 17
)

func CheckRequirements(config *rest.Config) (string, []string, error) {
failedRequirements := []string{}
func CheckKubernetesVersion(config *rest.Config) (bool, error) {
needPatch := false
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return "", failedRequirements, errors.WithMessage(err, "error creating API server client")
klog.Errorf("Error creating API server client: %v", err)
return needPatch, err
}
serverVersion, err := clientset.Discovery().ServerVersion()
if err != nil {
return "", failedRequirements, errors.WithMessage(err, "error obtaining API server version")
klog.Errorf("Error obtaining API server version: %v", err)
return needPatch, err
}
major, err := strconv.Atoi(serverVersion.Major)
if err != nil {
return serverVersion.String(), failedRequirements,
errors.WithMessagef(err, "error parsing API server major version %v", serverVersion.Major)
klog.Errorf("Error parsing API server major version %v", err)
return needPatch, err
}
var minor int
if strings.HasSuffix(serverVersion.Minor, "+") {
Expand All @@ -53,13 +56,19 @@ func CheckRequirements(config *rest.Config) (string, []string, error) {
minor, err = strconv.Atoi(serverVersion.Minor)
}
if err != nil {
return serverVersion.String(), failedRequirements,
errors.WithMessagef(err, "error parsing API server minor version %v", serverVersion.Minor)
klog.Errorf("Error parsing API server minor version %v", err)
return needPatch, err
}

if major < minK8sMajor || (major == minK8sMajor && minor < minK8sMinor) {
failedRequirements = append(failedRequirements,
fmt.Sprintf("Submariner requires Kubernetes %d.%d; your cluster is running %s.%s",
minK8sMajor, minK8sMinor, serverVersion.Major, serverVersion.Minor))
klog.Errorf("Submariner requires Kubernetes %d.%d; your cluster is running %s.%s",
minK8sMajor, minK8sMinor, serverVersion.Major, serverVersion.Minor)
return needPatch, fmt.Errorf("submariner requires Kubernetes %d.%d; your cluster is running %s.%s",
minK8sMajor, minK8sMinor, serverVersion.Major, serverVersion.Minor)
} else {
if minor < goodK8sMinor {
needPatch = true
}
return needPatch, nil
}
return serverVersion.String(), failedRequirements, nil
}
38 changes: 11 additions & 27 deletions controllers/deploy_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
submarinerv1a1 "github.com/submariner-io/submariner-operator/apis/submariner/v1alpha1"
"k8s.io/klog/v2"

"github.com/tkestack/knitnet-operator/controllers/checker"
"github.com/tkestack/knitnet-operator/controllers/components"
consts "github.com/tkestack/knitnet-operator/controllers/ensures"

Expand All @@ -31,17 +32,18 @@ import (
"github.com/tkestack/knitnet-operator/controllers/ensures/operator/submarinerop"
)

// var defaultComponents = []string{components.ServiceDiscovery, components.Connectivity}
// var validComponents = []string{components.ServiceDiscovery, components.Connectivity, components.Globalnet, components.Broker}

func (r *KnitnetReconciler) DeploySubmerinerBroker(instance *operatorv1alpha1.Knitnet) error {
brokerConfig := &instance.Spec.BrokerConfig

// if err := isValidComponents(instance); err != nil {
// klog.Errorf("Invalid components parameter: %v", err)
// return err
// }
needPatch, err := checker.CheckKubernetesVersion(r.Config)
if err != nil {
return err
}
if needPatch {
if err := checker.CreateOrUpdateEndpointslicesCRD(r.Client); err != nil {
return err
}
}

brokerConfig := &instance.Spec.BrokerConfig
if valid, err := isValidGlobalnetConfig(instance); !valid {
klog.Errorf("Invalid GlobalCIDR configuration: %v", err)
return err
Expand Down Expand Up @@ -83,24 +85,6 @@ func (r *KnitnetReconciler) DeploySubmerinerBroker(instance *operatorv1alpha1.Kn
return nil
}

// func isValidComponents(instance *operatorv1alpha1.Knitnet) error {
// componentSet := stringset.New(instance.Spec.BrokerConfig.ComponentArr...)
// validComponentSet := stringset.New(validComponents...)

// if componentSet.Size() < 1 {
// klog.Info("Use default components")
// instance.Spec.BrokerConfig.ComponentArr = defaultComponents
// return nil
// }

// for _, component := range componentSet.Elements() {
// if !validComponentSet.Contains(component) {
// return fmt.Errorf("unknown component: %s", component)
// }
// }
// return nil
// }

func isValidGlobalnetConfig(instance *operatorv1alpha1.Knitnet) (bool, error) {
brokerConfig := &instance.Spec.BrokerConfig
var err error
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ limitations under the License.
*/
package embeddedyamls

//go:generate go run generators/yamls2go.go ../../../../../ .
//go:generate go run generators/yamls2go.go ../../ .
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ var files = []string{
"manifests/config/rbac/networkplugin_syncer/service_account.yaml",
"manifests/config/rbac/networkplugin_syncer/cluster_role.yaml",
"manifests/config/rbac/networkplugin_syncer/cluster_role_binding.yaml",
"manifests/fix/crds/discovery.k8s.io_endpointslices.yaml",
}

// Reads all .yaml files in the crdDirectory
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion controllers/ensures/broker/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
consts "github.com/tkestack/knitnet-operator/controllers/ensures"
"github.com/tkestack/knitnet-operator/controllers/ensures/gateway"
"github.com/tkestack/knitnet-operator/controllers/ensures/lighthouse"
crdutils "github.com/tkestack/knitnet-operator/controllers/ensures/utils"
crdutils "github.com/tkestack/knitnet-operator/controllers/utils"
)

func Ensure(c client.Client, config *rest.Config, serviceDiscoveryEnabled, globalnetEnabled, crds bool) error {
Expand Down
4 changes: 2 additions & 2 deletions controllers/ensures/gateway/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/ensures/utils"
"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/utils"
)

// Ensure ensures that the required resources are deployed on the target system
Expand Down
4 changes: 2 additions & 2 deletions controllers/ensures/lighthouse/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/ensures/utils"
"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/utils"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
)

// Ensure creates the given service account
Expand Down
4 changes: 2 additions & 2 deletions controllers/ensures/operator/lighthouse/crds/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package crds
import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/ensures/utils"
"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/utils"
)

func Ensure(c client.Client) error {
Expand Down
2 changes: 1 addition & 1 deletion controllers/ensures/operator/lighthouse/scc/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package scc
import (
"k8s.io/client-go/rest"

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/scc"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/serviceaccount"

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
)

// Ensure functions updates or installs the operator CRDs in the cluster
Expand Down
4 changes: 2 additions & 2 deletions controllers/ensures/operator/submarinerop/crds/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package crds
import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/ensures/utils"
"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/utils"
)

// Ensure functions updates or installs the operator CRDs in the cluster
Expand Down
2 changes: 1 addition & 1 deletion controllers/ensures/operator/submarinerop/scc/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package scc
import (
"k8s.io/client-go/rest"

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/scc"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/serviceaccount"

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
)

// Ensure functions updates or installs the operator CRDs in the cluster
Expand Down
42 changes: 28 additions & 14 deletions controllers/join_broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
submariner "github.com/submariner-io/submariner-operator/apis/submariner/v1alpha1"

operatorv1alpha1 "github.com/tkestack/knitnet-operator/api/v1alpha1"
cmdVersion "github.com/tkestack/knitnet-operator/controllers/checker"
"github.com/tkestack/knitnet-operator/controllers/checker"
"github.com/tkestack/knitnet-operator/controllers/discovery/globalnet"
"github.com/tkestack/knitnet-operator/controllers/discovery/network"
consts "github.com/tkestack/knitnet-operator/controllers/ensures"
Expand Down Expand Up @@ -60,6 +60,16 @@ var nodeLabelBackoff wait.Backoff = wait.Backoff{
}

func (r *KnitnetReconciler) JoinSubmarinerCluster(instance *operatorv1alpha1.Knitnet) error {
needPatch, err := checker.CheckKubernetesVersion(r.Config)
if err != nil {
return err
}
if needPatch {
if err := checker.CreateOrUpdateEndpointslicesCRD(r.Client); err != nil {
return err
}
}

brokerInfo, err := SyncBrokerInfo(r.Client, r.Reader)
if err != nil {
return err
Expand Down Expand Up @@ -89,19 +99,19 @@ func (r *KnitnetReconciler) JoinSubmarinerCluster(instance *operatorv1alpha1.Kni
return err
}

_, failedRequirements, err := cmdVersion.CheckRequirements(r.Config)
// We display failed requirements even if an error occurred
if len(failedRequirements) > 0 {
klog.Info("The target cluster fails to meet Submariner's requirements:")
for i := range failedRequirements {
klog.Infof("* %s", (failedRequirements)[i])
}
return fmt.Errorf("the target cluster fails to meet Submariner's requirements")
}
if err != nil {
klog.Errorf("Unable to check all requirements: %v", err)
return err
}
// _, failedRequirements, err := cmdVersion.CheckRequirements(r.Config)
// // We display failed requirements even if an error occurred
// if len(failedRequirements) > 0 {
// klog.Info("The target cluster fails to meet Submariner's requirements:")
// for i := range failedRequirements {
// klog.Infof("* %s", (failedRequirements)[i])
// }
// return fmt.Errorf("the target cluster fails to meet Submariner's requirements")
// }
// if err != nil {
// klog.Errorf("Unable to check all requirements: %v", err)
// return err
// }
if brokerInfo.IsConnectivityEnabled() && joinConfig.LabelGateway {
if err := r.HandleNodeLabels(); err != nil {
klog.Errorf("Unable to set the gateway node up: %v", err)
Expand Down Expand Up @@ -227,18 +237,22 @@ func (r *KnitnetReconciler) AllocateAndUpdateGlobalCIDRConfigMap(c client.Client
func SyncBrokerInfo(c client.Client, reader client.Reader) (*broker.BrokerInfo, error) {
localConfigmap, err := broker.GetBrokerInfoConfigMap(reader)
if err != nil {
klog.Errorf("Get local cluster broker info configmap failed: %v", err)
return nil, err
}
brokerInfo, err := broker.NewFromString(localConfigmap.Data["brokerInfo"])
if err != nil {
klog.Errorf("New broker info configmap from string failed: %v", err)
return nil, err
}
brokerCluster, err := brokerInfo.GetBrokerAdministratorCluster()
if err != nil {
klog.Errorf("Get broker cluster administrator failed: %v", err)
return nil, err
}
brokerClusterConfigmap, err := broker.GetBrokerInfoConfigMap(brokerCluster.GetAPIReader())
if err != nil {
klog.Errorf("Get broker cluster broker info configmap failed: %v", err)
return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/tkestack/knitnet-operator/controllers/ensures/operator/common/embeddedyamls"
"github.com/tkestack/knitnet-operator/controllers/embeddedyamls"
)

type CRDUpdater interface {
Expand Down
13 changes: 13 additions & 0 deletions manifests/fix/crds/discovery.k8s.io_endpointslices.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: endpointslices.discovery.k8s.io
spec:
group: discovery.k8s.io
version: v1beta1
scope: Namespaced
names:
kind: EndpointSlice
plural: endpointslices
singular: endpointslice

0 comments on commit e708040

Please sign in to comment.