Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

WIP: providerid controller #17

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
58 changes: 55 additions & 3 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ import (
"flag"
"fmt"
"os"
"time"

"k8s.io/client-go/kubernetes"
"k8s.io/klog"

"github.com/openshift/cluster-api-provider-ovirt/pkg/apis"
"github.com/openshift/cluster-api-provider-ovirt/pkg/cloud/ovirt"
"github.com/openshift/cluster-api-provider-ovirt/pkg/cloud/ovirt/machine"
"github.com/openshift/cluster-api-provider-ovirt/pkg/cloud/ovirt/providerIDcontroller"

clusterapis "github.com/openshift/cluster-api/pkg/apis"
"github.com/openshift/cluster-api/pkg/client/clientset_generated/clientset"
capimachine "github.com/openshift/cluster-api/pkg/controller/machine"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
Expand All @@ -40,8 +43,42 @@ import (
func main() {
klog.InitFlags(nil)

watchNamespace := flag.String("namespace", "", "Namespace that the controller watches to reconcile machine-api objects. If unspecified, the controller watches for machine-api objects across all namespaces.")
metricsAddr := flag.String("metrics-addr", ":8080", "The address the metric endpoint binds to.")
watchNamespace := flag.String(
"namespace",
"",
"Namespace that the controller watches to reconcile machine-api objects. If unspecified, the controller watches for machine-api objects across all namespaces.",
)

metricsAddr := flag.String(
"metrics-addr",
":8080",
"The address the metric endpoint binds to.",
)

healthAddr := flag.String(
"health-addr",
":9440",
"The address for health checking.",
)

leaderElectResourceNamespace := flag.String(
"leader-elect-resource-namespace",
"",
"The namespace of resource object that is used for locking during leader election. If unspecified and running in cluster, defaults to the service account namespace for the controller. Required for leader-election outside of a cluster.",
)

leaderElect := flag.Bool(
"leader-elect",
false,
"Start a leader election client and gain leadership before executing the main loop. Enable this when running replicated components for high availability.",
)

leaderElectLeaseDuration := flag.Duration(
"leader-elect-lease-duration",
90*time.Second,
"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.",
)

flag.Parse()

log := logf.Log.WithName("ovirt-controller-manager")
Expand All @@ -55,7 +92,12 @@ func main() {

// Setup a Manager
opts := manager.Options{
MetricsBindAddress: *metricsAddr,
LeaderElection: *leaderElect,
LeaderElectionNamespace: *leaderElectResourceNamespace,
LeaderElectionID: "cluster-api-provider-ovirt-leader",
LeaseDuration: leaderElectLeaseDuration,
MetricsBindAddress: *metricsAddr,
HealthProbeBindAddress: *healthAddr,
}
if *watchNamespace != "" {
opts.Namespace = *watchNamespace
Expand Down Expand Up @@ -105,6 +147,16 @@ func main() {

capimachine.AddWithActuator(mgr, machineActuator)

providerIDcontroller.Add(mgr, manager.Options{})

if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
klog.Fatal(err)
}

if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
klog.Fatal(err)
}

if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
entryLog.Error(err, "unable to run manager")
os.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/openshift/cluster-api-provider-ovirt
go 1.12

require (
github.com/go-logr/logr v0.1.0
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
github.com/openshift/cluster-api v0.0.0-20191030113141-9a3a7bbe9258
github.com/ovirt/go-ovirt v0.0.0-20200428093010-9bcc4fd4e6c0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down Expand Up @@ -240,6 +241,7 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/soheilhy/cmux v0.1.3/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
Expand Down
2 changes: 0 additions & 2 deletions pkg/apis/ovirtprovider/v1beta1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ var (
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion}
)


// MachineSpecFromProviderSpec
func MachineSpecFromProviderSpec(providerSpec machinev1.ProviderSpec) (*OvirtMachineProviderSpec, error) {
if providerSpec.Value == nil {
Expand Down Expand Up @@ -122,4 +121,3 @@ func ProviderStatusFromRawExtension(rawExtension *runtime.RawExtension) (*OvirtM
klog.V(5).Infof("Got provider Status from raw extension: %+v", providerStatus)
return providerStatus, nil
}

37 changes: 4 additions & 33 deletions pkg/cloud/ovirt/clients/machineservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (is *InstanceService) InstanceDelete(id string) error {
if err != nil {
return err
}
err = util.PollImmediate(time.Second * 10, time.Minute * 5, func() (bool, error) {
err = util.PollImmediate(time.Second*10, time.Minute*5, func() (bool, error) {
vmResponse, err := vmService.Get().Send()
if err != nil {
return false, nil
Expand All @@ -233,50 +233,21 @@ func (is *InstanceService) InstanceDelete(id string) error {
return false, err
}

return vm.MustStatus() == ovirtsdk.VMSTATUS_DOWN, nil
return vm.MustStatus() == ovirtsdk.VMSTATUS_DOWN, nil
})
_, err = vmService.Remove().Send()

// poll till VM doesn't exist
err = util.PollImmediate(time.Second * 10, time.Minute * 5, func() (bool, error) {
err = util.PollImmediate(time.Second*10, time.Minute*5, func() (bool, error) {
_, err := vmService.Get().Send()
return err != nil, nil
return err != nil, nil
})
return err
}

func (is *InstanceService) GetInstanceList(opts *InstanceListOpts) ([]*Instance, error) {
var instanceList []*Instance

response, err := is.Connection.SystemService().VmsService().List().Send()
if err != nil {
klog.Errorf("Failed to fetch list of VMs for the cluster")
return nil, err
}

if vms, exists := response.Vms(); exists {
// TODO (rgolan) very inefficient get all query.
// Need to fetch all by cluster id and by Tag
// which is set the to openshift cluster Id
klog.Infof("Search return %d vms", len(vms.Slice()))
for _, vm := range vms.Slice() {
if cluster, ok := vm.Cluster(); ok {
id, _ := cluster.Id()
if id == is.ClusterId {
name, _ := vm.Name()
klog.V(5).Infof("Found VM: %v", name)
instanceList = append(instanceList, &Instance{Vm: vm})
}
}
}
}
return instanceList, nil
}

// Get VM by ID or Name
func (is *InstanceService) GetVm(machine machinev1.Machine) (instance *Instance, err error) {
if machine.Spec.ProviderID != nil && *machine.Spec.ProviderID != "" {
klog.Infof("Fetching VM by ID: %s", machine.Spec.ProviderID)
instance, err = is.GetVmByID(*machine.Spec.ProviderID)
if err == nil {
return instance, err
Expand Down
24 changes: 0 additions & 24 deletions pkg/cloud/ovirt/clients/machineservice_test.go

This file was deleted.

Loading