Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle PodDisruptionBudget policy/v1 #744

Merged
merged 3 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ require (
github.com/Azure/go-autorest/autorest v0.11.18
github.com/Azure/go-autorest/autorest/adal v0.9.13
github.com/Azure/go-autorest/autorest/to v0.3.0
github.com/Masterminds/semver v1.5.0
github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20180828111155-cad214d7d71f
github.com/aws/aws-sdk-go v1.23.13
github.com/cenkalti/backoff/v4 v4.1.0
github.com/davecgh/go-spew v1.1.1
github.com/go-git/go-git/v5 v5.4.2
github.com/go-openapi/spec v0.19.5
github.com/google/uuid v1.1.2
github.com/gophercloud/gophercloud v0.7.0
github.com/gophercloud/utils v0.0.0-20200204043447-9864b6f1f12f
github.com/onsi/ginkgo v1.16.2
Expand All @@ -36,8 +38,6 @@ require (
k8s.io/utils v0.0.0-20211116205334-6203023598ed
)

require github.com/google/uuid v1.1.2

require (
cloud.google.com/go v0.65.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUM
github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk=
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
Expand Down
50 changes: 50 additions & 0 deletions pkg/util/k8sutils/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright (c) 2022 SAP SE or an SAP affiliate company. All rights reserved.

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 k8sutils is used to provider helper consts and functions for k8s operations
package k8sutils

import (
"github.com/Masterminds/semver"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
)

var (
// ConstraintK8sGreaterEqual121 is a version constraint for versions >= 1.21.
ConstraintK8sGreaterEqual121 *semver.Constraints
)

func init() {
var err error

ConstraintK8sGreaterEqual121, err = semver.NewConstraint(">= 1.21")
utilruntime.Must(err)
}

// CheckVersionMeetsConstraint returns true if the <version> meets the <constraint>.
func CheckVersionMeetsConstraint(version, constraint string) (bool, error) {
c, err := semver.NewConstraint(constraint)
if err != nil {
return false, err
}

v, err := semver.NewVersion(version)
if err != nil {
return false, err
}

return c.Check(v), nil
}
30 changes: 28 additions & 2 deletions pkg/util/provider/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ import (
"strconv"
"time"

"github.com/Masterminds/semver"
machinescheme "github.com/gardener/machine-controller-manager/pkg/client/clientset/versioned/scheme"
machineinformers "github.com/gardener/machine-controller-manager/pkg/client/informers/externalversions"
coreclientbuilder "github.com/gardener/machine-controller-manager/pkg/util/clientbuilder/core"
machineclientbuilder "github.com/gardener/machine-controller-manager/pkg/util/clientbuilder/machine"
"github.com/gardener/machine-controller-manager/pkg/util/k8sutils"
machinecontroller "github.com/gardener/machine-controller-manager/pkg/util/provider/machinecontroller"
coreinformers "k8s.io/client-go/informers"
kubescheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -50,6 +52,8 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
policyv1informers "k8s.io/client-go/informers/policy/v1"
policyv1beta1informers "k8s.io/client-go/informers/policy/v1beta1"
"k8s.io/client-go/kubernetes"
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/pkg/version"
Expand Down Expand Up @@ -225,6 +229,15 @@ func StartControllers(s *options.MCServer,
klog.Fatal(err)
}

targetServerVersion, err := targetCoreClient.Discovery().ServerVersion()
if err != nil {
return err
}
targetKubernetesVersion, err := semver.NewVersion(targetServerVersion.GitVersion)
if err != nil {
return err
}

if availableResources[machineGVR] {
klog.V(5).Infof("Creating shared informers; resync interval: %v", s.MinResyncPeriod)

Expand All @@ -247,6 +260,17 @@ func StartControllers(s *options.MCServer,
s.MinResyncPeriod.Duration,
)

var (
pdbV1Informer policyv1informers.PodDisruptionBudgetInformer
pdbV1beta1Informer policyv1beta1informers.PodDisruptionBudgetInformer
)

if k8sutils.ConstraintK8sGreaterEqual121.Check(targetKubernetesVersion) {
pdbV1Informer = targetCoreInformerFactory.Policy().V1().PodDisruptionBudgets()
} else {
pdbV1beta1Informer = targetCoreInformerFactory.Policy().V1beta1().PodDisruptionBudgets()
}

// All shared informers are v1alpha1 API level
machineSharedInformers := controlMachineInformerFactory.Machine().V1alpha1()

Expand All @@ -261,14 +285,16 @@ func StartControllers(s *options.MCServer,
targetCoreInformerFactory.Core().V1().PersistentVolumes(),
controlCoreInformerFactory.Core().V1().Secrets(),
targetCoreInformerFactory.Core().V1().Nodes(),
targetCoreInformerFactory.Policy().V1beta1().PodDisruptionBudgets(),
pdbV1beta1Informer,
pdbV1Informer,
targetCoreInformerFactory.Storage().V1().VolumeAttachments(),
machineSharedInformers.MachineClasses(),
machineSharedInformers.Machines(),
recorder,
s.SafetyOptions,
s.NodeConditions,
s.BootstrapTokenAuthExtraGroups,
targetKubernetesVersion,
)
if err != nil {
return err
Expand All @@ -290,7 +316,7 @@ func StartControllers(s *options.MCServer,
}

// TODO: In general, any controller checking this needs to be dynamic so
// users don't have to restart their controller manager if they change the apiserver.
// users don't have to restart their controller manager if they change the apiserver.
// Until we get there, the structure here needs to be exposed for the construction of a proper ControllerContext.
func getAvailableResources(clientBuilder coreclientbuilder.ClientBuilder) (map[schema.GroupVersionResource]bool, error) {
var discoveryClient discovery.DiscoveryInterface
Expand Down
Loading