Skip to content

Commit

Permalink
Merge pull request #184 from slintes/metal3_remediation
Browse files Browse the repository at this point in the history
Metal3 remediation backport
  • Loading branch information
openshift-merge-robot committed Feb 12, 2023
2 parents 16303ce + 47fbab9 commit 0a75189
Show file tree
Hide file tree
Showing 447 changed files with 69,635 additions and 779 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Expand Up @@ -11,3 +11,6 @@ FROM registry.ci.openshift.org/ocp/4.13:base
# rpm -V $INSTALL_PKGS && \
# yum clean all
COPY --from=builder /go/src/github.com/openshift/cluster-api-provider-baremetal/machine-controller-manager /
COPY --from=builder /go/src/github.com/openshift/cluster-api-provider-baremetal/manifests /manifests

LABEL io.openshift.release.operator=true
24 changes: 23 additions & 1 deletion Makefile
Expand Up @@ -3,6 +3,10 @@
IMG ?= controller:latest
GOPATH=$(shell go env GOPATH)

MOCKGEN := $(GOPATH)/bin/mockgen
$(MOCKGEN): # Build mockgen
go build -tags=tools -o $(GOPATH)/bin github.com/golang/mock/mockgen

.PHONY: build
build:
@mkdir -p bin
Expand Down Expand Up @@ -43,9 +47,23 @@ vet:

# Generate code
.PHONY: generate
generate:
generate: $(MOCKGEN)
go generate ./pkg/... ./cmd/...

$(MOCKGEN) \
-destination=./pkg/baremetal/mocks/zz_generated.metal3remediation_manager.go \
-source=./pkg/baremetal/metal3remediation_manager.go \
-package=baremetal_mocks \
-copyright_file=./hack/boilerplate.go.txt \
RemediationManagerInterface

$(MOCKGEN) \
-destination=./pkg/baremetal/mocks/zz_generated.manager_factory.go \
-source=./pkg/baremetal/manager_factory.go \
-package=baremetal_mocks \
-copyright_file=./hack/boilerplate.go.txt \
ManagerFactoryInterface

.PHONY: generate-check
generate-check:
./hack/generate.sh
Expand All @@ -67,3 +85,7 @@ vendor:
go mod tidy
go mod vendor
go mod verify

.PHONY: metal3-crds
metal3-crds:
./hack/fetch-metal3-crds.sh
54 changes: 53 additions & 1 deletion cmd/manager/main.go
Expand Up @@ -23,24 +23,35 @@ import (
"time"

bmoapis "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
capm3apis "github.com/metal3-io/cluster-api-provider-metal3/api/v1beta1"
machinev1beta1 "github.com/openshift/api/machine/v1beta1"
"github.com/openshift/cluster-api-provider-baremetal/pkg/apis"
"github.com/openshift/cluster-api-provider-baremetal/pkg/baremetal"
"github.com/openshift/cluster-api-provider-baremetal/pkg/cloud/baremetal/actuators/machine"
"github.com/openshift/cluster-api-provider-baremetal/pkg/controller"
"github.com/openshift/cluster-api-provider-baremetal/pkg/controller/metal3remediation"
"github.com/openshift/cluster-api-provider-baremetal/pkg/manager/wrapper"
maomachine "github.com/openshift/machine-api-operator/pkg/controller/machine"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)

const (
// Align with MachineWebhookPort in https://github.com/openshift/machine-api-operator/blob/master/pkg/operator/sync.go
// or import from there when relevant change was merged
defaultWebhookPort = 8440
defaultWebhookCertdir = "/etc/machine-api-operator/tls"
)

// The default durations for the leader election operations.
var (
leaseDuration = 120 * time.Second
Expand Down Expand Up @@ -87,6 +98,16 @@ func main() {
leaseDuration,
"The duration that non-leader candidates will wait after observing a leadership renewal until attempting to acquire leadership of a led but unrenewed leader slot. This is effectively the maximum duration that a leader can be stopped before it is replaced by another candidate. This is only applicable if leader election is enabled.",
)

webhookEnabled := flag.Bool("webhook-enabled", true,
"Webhook server, enabled by default. When enabled, the manager will run a webhook server.")

webhookPort := flag.Int("webhook-port", defaultWebhookPort,
"Webhook Server port, only used when webhook-enabled is true.")

webhookCertdir := flag.String("webhook-cert-dir", defaultWebhookCertdir,
"Webhook cert dir, only used when webhook-enabled is true.")

flag.Parse()

log := logf.Log.WithName("baremetal-controller-manager")
Expand Down Expand Up @@ -146,6 +167,10 @@ func main() {
panic(err)
}

if err := capm3apis.AddToScheme(mgr.GetScheme()); err != nil {
panic(err)
}

// the manager wrapper will add an extra Watch to the controller
maomachine.AddWithActuator(wrapper.New(mgr), machineActuator)

Expand All @@ -154,6 +179,33 @@ func main() {
os.Exit(1)
}

// Set up the context that's going to be used in controllers and for the manager.
ctx := signals.SetupSignalHandler()

if err := (&metal3remediation.Metal3RemediationReconciler{
Client: mgr.GetClient(),
ManagerFactory: baremetal.NewManagerFactory(mgr.GetClient()),
Log: ctrl.Log.WithName("controllers").WithName("Metal3Remediation"),
}).SetupWithManager(ctx, mgr); err != nil {
log.Error(err, "unable to create controller", "controller", "Metal3Remediation")
os.Exit(1)
}

if *webhookEnabled {
mgr.GetWebhookServer().Port = *webhookPort
mgr.GetWebhookServer().CertDir = *webhookCertdir

if err := (&capm3apis.Metal3Remediation{}).SetupWebhookWithManager(mgr); err != nil {
log.Error(err, "unable to create webhook", "webhook", "Metal3Remediation")
os.Exit(1)
}

if err := (&capm3apis.Metal3RemediationTemplate{}).SetupWebhookWithManager(mgr); err != nil {
log.Error(err, "unable to create webhook", "webhook", "Metal3RemediationTemplate")
os.Exit(1)
}
}

if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
klog.Fatal(err)
}
Expand All @@ -162,7 +214,7 @@ func main() {
klog.Fatal(err)
}

if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
if err := mgr.Start(ctx); err != nil {
entryLog.Error(err, "unable to run manager")
os.Exit(1)
}
Expand Down
178 changes: 178 additions & 0 deletions config/crd/infrastructure.cluster.x-k8s.io_metal3remediations.yaml
@@ -0,0 +1,178 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.1
creationTimestamp: null
name: metal3remediations.infrastructure.cluster.x-k8s.io
spec:
group: infrastructure.cluster.x-k8s.io
names:
categories:
- cluster-api
kind: Metal3Remediation
listKind: Metal3RemediationList
plural: metal3remediations
shortNames:
- m3r
- m3remediation
singular: metal3remediation
scope: Namespaced
versions:
- additionalPrinterColumns:
- description: How many times remediation controller should attempt to remediate
the host
jsonPath: .spec.strategy.retryLimit
name: Retry limit
type: string
- description: How many times remediation controller has tried to remediate the
node
jsonPath: .status.retryCount
name: Retry count
type: string
- description: Timestamp of the last remediation attempt
jsonPath: .status.lastRemediated
name: Last Remediated
type: string
- description: Type of the remediation strategy
jsonPath: .spec.strategy.type
name: Strategy
type: string
- description: Phase of the remediation
jsonPath: .status.phase
name: Phase
type: string
name: v1alpha5
schema:
openAPIV3Schema:
description: Metal3Remediation is the Schema for the metal3remediations API.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: Metal3RemediationSpec defines the desired state of Metal3Remediation.
properties:
strategy:
description: Strategy field defines remediation strategy.
properties:
retryLimit:
description: Sets maximum number of remediation retries.
type: integer
timeout:
description: Sets the timeout between remediation retries.
type: string
type:
description: Type of remediation.
type: string
type: object
type: object
status:
description: Metal3RemediationStatus defines the observed state of Metal3Remediation.
properties:
lastRemediated:
description: LastRemediated identifies when the host was last remediated
format: date-time
type: string
phase:
description: Phase represents the current phase of machine remediation.
E.g. Pending, Running, Done etc.
type: string
retryCount:
description: RetryCount can be used as a counter during the remediation.
Field can hold number of reboots etc.
type: integer
type: object
type: object
served: true
storage: false
subresources:
status: {}
- additionalPrinterColumns:
- description: How many times remediation controller should attempt to remediate
the host
jsonPath: .spec.strategy.retryLimit
name: Retry limit
type: string
- description: How many times remediation controller has tried to remediate the
node
jsonPath: .status.retryCount
name: Retry count
type: string
- description: Timestamp of the last remediation attempt
jsonPath: .status.lastRemediated
name: Last Remediated
type: string
- description: Type of the remediation strategy
jsonPath: .spec.strategy.type
name: Strategy
type: string
- description: Phase of the remediation
jsonPath: .status.phase
name: Phase
type: string
name: v1beta1
schema:
openAPIV3Schema:
description: Metal3Remediation is the Schema for the metal3remediations API.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: Metal3RemediationSpec defines the desired state of Metal3Remediation.
properties:
strategy:
description: Strategy field defines remediation strategy.
properties:
retryLimit:
description: Sets maximum number of remediation retries.
type: integer
timeout:
description: Sets the timeout between remediation retries.
type: string
type:
description: Type of remediation.
type: string
type: object
type: object
status:
description: Metal3RemediationStatus defines the observed state of Metal3Remediation.
properties:
lastRemediated:
description: LastRemediated identifies when the host was last remediated
format: date-time
type: string
phase:
description: Phase represents the current phase of machine remediation.
E.g. Pending, Running, Done etc.
type: string
retryCount:
description: RetryCount can be used as a counter during the remediation.
Field can hold number of reboots etc.
type: integer
type: object
type: object
served: true
storage: true
subresources:
status: {}

0 comments on commit 0a75189

Please sign in to comment.