Skip to content

Commit

Permalink
Merge pull request #74 from cynepco3hahue/deploy_master_mhc_mdb
Browse files Browse the repository at this point in the history
Deploy master mhc mdb
  • Loading branch information
Artyom Lukianov authored Sep 10, 2019
2 parents c861e8f + aa31f4d commit 14e0dae
Show file tree
Hide file tree
Showing 46 changed files with 7,966 additions and 7 deletions.
2 changes: 2 additions & 0 deletions cmd/machine-remediation-operator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ go_library(
"//pkg/consts:go_default_library",
"//pkg/controllers:go_default_library",
"//pkg/operator:go_default_library",
"//pkg/utils/mapper:go_default_library",
"//pkg/version:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/openshift/api/config/v1:go_default_library",
"//vendor/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/sigs.k8s.io/controller-runtime/pkg/cache:go_default_library",
Expand Down
9 changes: 8 additions & 1 deletion cmd/machine-remediation-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"runtime"

"github.com/golang/glog"
osconfigv1 "github.com/openshift/api/config/v1"

extv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -13,6 +14,7 @@ import (
"kubevirt.io/machine-remediation-operator/pkg/consts"
"kubevirt.io/machine-remediation-operator/pkg/controllers"
"kubevirt.io/machine-remediation-operator/pkg/operator"
"kubevirt.io/machine-remediation-operator/pkg/utils/mapper"
"kubevirt.io/machine-remediation-operator/pkg/version"

"sigs.k8s.io/controller-runtime/pkg/cache"
Expand Down Expand Up @@ -44,7 +46,8 @@ func main() {
namespaces = append(namespaces, *namespace)
}
opts := manager.Options{
NewCache: cache.MultiNamespacedCacheBuilder(namespaces),
NewCache: cache.MultiNamespacedCacheBuilder(namespaces),
MapperProvider: mapper.NewDynamicRESTMapper,
}

// Create a new Cmd to provide shared dependencies and start components
Expand All @@ -64,6 +67,10 @@ func main() {
glog.Fatal(err)
}

if err := osconfigv1.AddToScheme(mgr.GetScheme()); err != nil {
glog.Fatal(err)
}

// Setup all Controllers
if err := controllers.AddToManager(mgr, opts, operator.Add); err != nil {
glog.Fatal(err)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/metal3-io/baremetal-operator v0.0.0-20190705194231-6d5a9e11b6d0
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.5.0
github.com/openshift/api v0.0.0-00010101000000-000000000000
github.com/openshift/cluster-api v0.0.0-20190503193905-cad0f8326cd2 // indirect
github.com/operator-framework/operator-lifecycle-manager v0.0.0-20190822225318-a96b2b847d08
github.com/prometheus/client_golang v1.0.0 // indirect
Expand All @@ -35,7 +36,7 @@ require (

replace github.com/metal3-io/baremetal-operator => github.com/openshift/baremetal-operator v0.0.0-20190727010256-d905ab5eaddd

replace github.com/openshift/api => github.com/openshift/api v3.9.1-0.20190730142803-0922aa5a655b+incompatible
replace github.com/openshift/api => github.com/openshift/api v3.9.1-0.20190904155310-a25bb2adc83e+incompatible

replace github.com/openshift/client-go => github.com/openshift/client-go v0.0.0-20190721020503-a85ea6a6b3a5

Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/openshift/api v3.9.1-0.20190730142803-0922aa5a655b+incompatible/go.mod h1:dh9o4Fs58gpFXGSYfnVxGR9PnV53I8TW84pQaJDdGiY=
github.com/openshift/api v3.9.1-0.20190904155310-a25bb2adc83e+incompatible h1:p/By3VvLr3qKUgScmVzv0X8OU7cgIBV2Hf215Ab3ve0=
github.com/openshift/api v3.9.1-0.20190904155310-a25bb2adc83e+incompatible/go.mod h1:dh9o4Fs58gpFXGSYfnVxGR9PnV53I8TW84pQaJDdGiY=
github.com/openshift/baremetal-operator v0.0.0-20190727010256-d905ab5eaddd h1:MRVvynv9x5epi9L38KnT3YXDVbF5Gelqn/rFWgg06RY=
github.com/openshift/baremetal-operator v0.0.0-20190727010256-d905ab5eaddd/go.mod h1:cXwn0hhgHpORjBasg0RnZwhKaJGy9+r6qgj0HCXrs/Y=
github.com/openshift/client-go v0.0.0-20190721020503-a85ea6a6b3a5/go.mod h1:6rzn+JTr7+WYS2E1TExP4gByoABxMznR6y2SnUIkmxk=
Expand Down
16 changes: 16 additions & 0 deletions manifests/generated/machine-remediation-operator-csv.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ spec:
- clusterrolebindings
verbs:
- '*'
- apiGroups:
- config.openshift.io
resources:
- infrastructures
- infrastructures/status
verbs:
- get
- list
- watch
- apiGroups:
- machineremediation.kubevirt.io
resources:
- machinedisruptionbudgets
- machinehealthchecks
verbs:
- '*'
serviceAccountName: machine-remediation-operator
deployments:
- name: machine-remediation-operator
Expand Down
16 changes: 16 additions & 0 deletions manifests/generated/machine-remediation-operator.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ rules:
- clusterrolebindings
verbs:
- '*'
- apiGroups:
- config.openshift.io
resources:
- infrastructures
- infrastructures/status
verbs:
- get
- list
- watch
- apiGroups:
- machineremediation.kubevirt.io
resources:
- machinedisruptionbudgets
- machinehealthchecks
verbs:
- '*'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const (

const (
// RemediationStrategyTypeReboot contains name of the reboot remediation strategy
RemediationStrategyTypeReboot = "reboot"
RemediationStrategyTypeReboot RemediationStrategyType = "reboot"
// RemediationStrategyTypeReCreate contains name of the re-create remediation strategy
RemediationStrategyTypeReCreate = "recreate"
RemediationStrategyTypeReCreate RemediationStrategyType = "recreate"
)

// +genclient
Expand Down
4 changes: 4 additions & 0 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const (
ControllerMachineRemediation = "machine-remediation"
//MasterRoleLabel contains master role label
MasterRoleLabel = "node-role.kubernetes.io/master"
// MasterMachineHealthCheck contains the MachineHealthCheck name for master nodes
MasterMachineHealthCheck = "masters"
// MasterMachineDisruptionBudget contains the MachineDisruptionBudget name for master nodes
MasterMachineDisruptionBudget = "masters"
// NamespaceOpenshiftMachineAPI contains namespace name for the machine-api componenets under the OpenShift cluster
NamespaceOpenshiftMachineAPI = "openshift-machine-api"
)
3 changes: 3 additions & 0 deletions pkg/operator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"//pkg/operator/components:go_default_library",
"//vendor/github.com/ghodss/yaml:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/openshift/api/config/v1:go_default_library",
"//vendor/k8s.io/api/apps/v1:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/api/rbac/v1:go_default_library",
Expand All @@ -39,6 +40,8 @@ go_test(
deps = [
"//pkg/apis/machineremediation/v1alpha1:go_default_library",
"//pkg/consts:go_default_library",
"//pkg/utils/testing:go_default_library",
"//vendor/github.com/openshift/api/config/v1:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/k8s.io/api/apps/v1:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
Expand Down
3 changes: 3 additions & 0 deletions pkg/operator/components/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ go_library(
"components.go",
"csv.go",
"deployments.go",
"mdb.go",
"mhc.go",
"mro.go",
"rbac.go",
],
importpath = "kubevirt.io/machine-remediation-operator/pkg/operator/components",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/machineremediation/v1alpha1:go_default_library",
"//pkg/consts:go_default_library",
"//vendor/github.com/blang/semver:go_default_library",
"//vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1:go_default_library",
"//vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/lib/version:go_default_library",
Expand Down
35 changes: 35 additions & 0 deletions pkg/operator/components/mdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package components

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"

mrv1 "kubevirt.io/machine-remediation-operator/pkg/apis/machineremediation/v1alpha1"
"kubevirt.io/machine-remediation-operator/pkg/consts"
)

// NewMastersMachineDisruptionBudget retruns new MachineDisruptionBudget object for master nodes
func NewMastersMachineDisruptionBudget(name string, namespace string, operatorVersion string) *mrv1.MachineDisruptionBudget {
return &mrv1.MachineDisruptionBudget{
TypeMeta: metav1.TypeMeta{
APIVersion: "machineremediation.kubevirt.io/v1alpha1",
Kind: "MachineDisruptionBudget",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
mrv1.SchemeGroupVersion.Group: "",
mrv1.SchemeGroupVersion.Group + "/version": operatorVersion,
},
},
Spec: mrv1.MachineDisruptionBudgetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
consts.MasterRoleLabel: "",
},
},
MinAvailable: pointer.Int32Ptr(1),
},
}
}
35 changes: 35 additions & 0 deletions pkg/operator/components/mhc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package components

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

mrv1 "kubevirt.io/machine-remediation-operator/pkg/apis/machineremediation/v1alpha1"
"kubevirt.io/machine-remediation-operator/pkg/consts"
)

// NewMastersMachineHealthCheck retruns new MachineHealthCheck object for master nodes
func NewMastersMachineHealthCheck(name string, namespace string, operatorVersion string) *mrv1.MachineHealthCheck {
rebootStrategy := mrv1.RemediationStrategyTypeReboot
return &mrv1.MachineHealthCheck{
TypeMeta: metav1.TypeMeta{
APIVersion: "machineremediation.kubevirt.io/v1alpha1",
Kind: "MachineHealthCheck",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
mrv1.SchemeGroupVersion.Group: "",
mrv1.SchemeGroupVersion.Group + "/version": operatorVersion,
},
},
Spec: mrv1.MachineHealthCheckSpec{
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
consts.MasterRoleLabel: "",
},
},
RemediationStrategy: &rebootStrategy,
},
}
}
26 changes: 26 additions & 0 deletions pkg/operator/components/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,32 @@ var (
rbacv1.VerbAll,
},
},
{
APIGroups: []string{
"config.openshift.io",
},
Resources: []string{
"infrastructures",
"infrastructures/status",
},
Verbs: []string{
"get",
"list",
"watch",
},
},
{
APIGroups: []string{
"machineremediation.kubevirt.io",
},
Resources: []string{
"machinedisruptionbudgets",
"machinehealthchecks",
},
Verbs: []string{
rbacv1.VerbAll,
},
},
},
}
)
Expand Down
48 changes: 48 additions & 0 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/golang/glog"
osconfigv1 "github.com/openshift/api/config/v1"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -147,13 +148,15 @@ func (r *ReconcileMachineRemediationOperator) Reconcile(request reconcile.Reques
}

func (r *ReconcileMachineRemediationOperator) createOrUpdateComponents(mro *mrv1.MachineRemediationOperator) error {
// create CRD's
for _, crd := range components.CRDS {
glog.Infof("Creating or updating CRD %q", crd)
if err := r.createOrUpdateCustomResourceDefinition(crd); err != nil {
return err
}
}

// create RBAC and deployment
for _, component := range components.Components {
glog.Infof("Creating objects for component %q", component)
if err := r.createOrUpdateServiceAccount(component, consts.NamespaceOpenshiftMachineAPI); err != nil {
Expand Down Expand Up @@ -181,10 +184,43 @@ func (r *ReconcileMachineRemediationOperator) createOrUpdateComponents(mro *mrv1
return err
}
}

baremetal, err := r.isBareMetal()
if err != nil {
return err
}

// deploy masters MachineHealthCheck and MachineDisruptionBudget only for BareMetal environment
if baremetal {
if err := r.createOrUpdateMachineHealthCheck(consts.MasterMachineHealthCheck, consts.NamespaceOpenshiftMachineAPI); err != nil {
return err
}

if err := r.createOrUpdateMachineDisruptionBudget(consts.MasterMachineDisruptionBudget, consts.NamespaceOpenshiftMachineAPI); err != nil {
return err
}
}

return nil
}

func (r *ReconcileMachineRemediationOperator) deleteComponents() error {
baremetal, err := r.isBareMetal()
if err != nil {
return err
}

// delete masters MachineHealthCheck and MachineDisruptionBudget only for BareMetal environment
if baremetal {
if err := r.deleteMachineHealthCheck(consts.MasterMachineHealthCheck, consts.NamespaceOpenshiftMachineAPI); err != nil {
return err
}

if err := r.deleteMachineDisruptionBudget(consts.MasterMachineDisruptionBudget, consts.NamespaceOpenshiftMachineAPI); err != nil {
return err
}
}

for _, component := range components.Components {
glog.Infof("Deleting objets for component %q", component)
if err := r.deleteDeployment(component, consts.NamespaceOpenshiftMachineAPI); err != nil {
Expand Down Expand Up @@ -284,6 +320,18 @@ func (r *ReconcileMachineRemediationOperator) statusProgressing(mro *mrv1.Machin
return r.client.Status().Update(context.TODO(), mro)
}

func (r *ReconcileMachineRemediationOperator) isBareMetal() (bool, error) {
infrastructure, err := r.getInfrastructure("cluster")
if err != nil {
return false, err
}

if infrastructure.Status.Platform == osconfigv1.BareMetalPlatformType {
return true, nil
}
return false, nil
}

func addFinalizer(mro *mrv1.MachineRemediationOperator) {
if !hasFinalizer(mro) {
mro.Finalizers = append(mro.Finalizers, machineRemediationOperatorFinalizer)
Expand Down
Loading

0 comments on commit 14e0dae

Please sign in to comment.