From 89f6addbd9a4f00b8ff3b37603389171bc9a3b98 Mon Sep 17 00:00:00 2001 From: Enxebre Date: Tue, 16 Oct 2018 14:27:39 +0200 Subject: [PATCH] add log-level flag and makes create idempotent --- Makefile | 4 -- cmd/manager/main.go | 69 ++++++++++++++++++- .../v1alpha1/register.go | 38 ---------- .../awsmachineproviderconfig_types.go | 3 +- .../awsproviderconfig/v1alpha1/register.go | 9 ++- pkg/cloud/aws/actuators/machine/actuator.go | 21 ++++-- pkg/cloud/aws/actuators/machine/config.go | 22 ++---- pkg/controller/add_machine.go | 67 +++--------------- 8 files changed, 104 insertions(+), 129 deletions(-) delete mode 100644 pkg/apis/awsproviderconfig.k8s.io/v1alpha1/register.go diff --git a/Makefile b/Makefile index b0839a9ca1..e3f1061a26 100644 --- a/Makefile +++ b/Makefile @@ -94,10 +94,6 @@ vet: ## Apply go vet to all go files help: @grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' - - - - # Build manager binary manager: go build -o bin/manager sigs.k8s.io/cluster-api-provider-aws/cmd/manager diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 816d0fbc41..7c2323d593 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -17,8 +17,6 @@ limitations under the License. package main import ( - "log" - _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" "sigs.k8s.io/cluster-api-provider-aws/pkg/apis" "sigs.k8s.io/cluster-api-provider-aws/pkg/controller" @@ -26,9 +24,34 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/runtime/signals" clusterapis "sigs.k8s.io/cluster-api/pkg/apis" + "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset" + "k8s.io/client-go/kubernetes" + machineactuator "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/actuators/machine" + "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1alpha1" + "github.com/golang/glog" + log "github.com/sirupsen/logrus" + awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/client" + "os" + "github.com/spf13/pflag" +) + +var ( + logLevel string ) +const ( + defaultLogLevel = "debug" +) + +func init() { + pflag.CommandLine.StringVar(&logLevel, "log-level", defaultLogLevel, "Log level (debug,info,warn,error,fatal)") +} + func main() { + // the following line exists to make glog happy, for more information, see: https://github.com/kubernetes/kubernetes/issues/17162 + //flag.CommandLine.Parse([]string{}) + pflag.Parse() + // Get a config to talk to the apiserver cfg, err := config.GetConfig() if err != nil { @@ -52,6 +75,7 @@ func main() { log.Fatal(err) } + initActuator(mgr) // Setup all Controllers if err := controller.AddToManager(mgr); err != nil { log.Fatal(err) @@ -62,3 +86,44 @@ func main() { // Start the Cmd log.Fatal(mgr.Start(signals.SetupSignalHandler())) } + +func initActuator(m manager.Manager) { + config := m.GetConfig() + client, err := clientset.NewForConfig(config) + if err != nil { + glog.Fatalf("Could not create client for talking to the apiserver: %v", err) + } + + + kubeClient, err := kubernetes.NewForConfig(config) + if err != nil { + glog.Fatalf("Could not create kubernetes client to talk to the apiserver: %v", err) + } + + log.SetOutput(os.Stdout) + if lvl, err := log.ParseLevel(logLevel); err != nil { + log.Panic(err) + } else { + log.SetLevel(lvl) + } + + logger := log.WithField("controller", "awsMachine") + + codec, err := v1alpha1.NewCodec() + if err != nil { + glog.Fatal(err) + } + + params := machineactuator.ActuatorParams{ + ClusterClient: client, + KubeClient: kubeClient, + AwsClientBuilder: awsclient.NewClient, + Logger: logger, + Codec: codec, + } + + machineactuator.MachineActuator, err = machineactuator.NewActuator(params) + if err != nil { + glog.Fatalf("Could not create AWS machine actuator: %v", err) + } +} \ No newline at end of file diff --git a/pkg/apis/awsproviderconfig.k8s.io/v1alpha1/register.go b/pkg/apis/awsproviderconfig.k8s.io/v1alpha1/register.go deleted file mode 100644 index 9c6a48db6e..0000000000 --- a/pkg/apis/awsproviderconfig.k8s.io/v1alpha1/register.go +++ /dev/null @@ -1,38 +0,0 @@ -/* -Copyright 2018 The Kubernetes Authors. - -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. -*/ - -// NOTE: Boilerplate only. Ignore this file. - -// Package v1alpha1 contains API Schema definitions for the awsproviderconfig.k8s.io v1alpha1 API group -// +k8s:openapi-gen=true -// +k8s:deepcopy-gen=package,register -// +k8s:conversion-gen=sigs.k8s.io/cluster-api-provider-aws/migration/pkg/apis/awsproviderconfig.k8s.io -// +k8s:defaulter-gen=TypeMeta -// +groupName=awsproviderconfig.k8s.io.k8s.io -package v1alpha1 - -import ( - "k8s.io/apimachinery/pkg/runtime/schema" - "sigs.k8s.io/controller-runtime/pkg/runtime/scheme" -) - -var ( - // SchemeGroupVersion is group version used to register these objects - SchemeGroupVersion = schema.GroupVersion{Group: "awsproviderconfig.k8s.io.k8s.io", Version: "v1alpha1"} - - // SchemeBuilder is used to add go types to the GroupVersionKind scheme - SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion} -) diff --git a/pkg/apis/awsproviderconfig/v1alpha1/awsmachineproviderconfig_types.go b/pkg/apis/awsproviderconfig/v1alpha1/awsmachineproviderconfig_types.go index 400c853d71..f8763079f8 100644 --- a/pkg/apis/awsproviderconfig/v1alpha1/awsmachineproviderconfig_types.go +++ b/pkg/apis/awsproviderconfig/v1alpha1/awsmachineproviderconfig_types.go @@ -43,8 +43,7 @@ type AWSMachineProviderConfigList struct { } func init() { - SchemeBuilder.Register(&AWSMachineProviderConfig{}, &AWSMachineProviderConfigList{}) - SchemeBuilder.Register(&AWSMachineProviderStatus{}) + SchemeBuilder.Register(&AWSMachineProviderConfigList{}, &AWSMachineProviderConfig{}, &AWSMachineProviderStatus{}) } // +genclient diff --git a/pkg/apis/awsproviderconfig/v1alpha1/register.go b/pkg/apis/awsproviderconfig/v1alpha1/register.go index e0224670a3..2ed06e8afa 100644 --- a/pkg/apis/awsproviderconfig/v1alpha1/register.go +++ b/pkg/apis/awsproviderconfig/v1alpha1/register.go @@ -19,7 +19,7 @@ limitations under the License. // Package v1alpha1 contains API Schema definitions for the awsproviderconfig v1alpha1 API group // +k8s:openapi-gen=true // +k8s:deepcopy-gen=package,register -// +k8s:conversion-gen=sigs.k8s.io/cluster-api-provider-aws/migration/pkg/apis/awsproviderconfig +// +k8s:conversion-gen=sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig // +k8s:defaulter-gen=TypeMeta // +groupName=awsproviderconfig.k8s.io package v1alpha1 @@ -42,10 +42,9 @@ var ( SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion} ) // -//func init() { -// SchemeBuilder.Register(&AWSMachineProviderConfig{}) -// SchemeBuilder.Register(&AWSMachineProviderStatus{}) -//} +func init() { + SchemeBuilder.Register(&AWSMachineProviderConfigList{}, &AWSMachineProviderConfig{}, &AWSMachineProviderStatus{}) +} // AWSProviderConfigCodec is a runtime codec for the provider configuration // +k8s:deepcopy-gen=false diff --git a/pkg/cloud/aws/actuators/machine/actuator.go b/pkg/cloud/aws/actuators/machine/actuator.go index f9e5248f9d..5a24161eda 100644 --- a/pkg/cloud/aws/actuators/machine/actuator.go +++ b/pkg/cloud/aws/actuators/machine/actuator.go @@ -54,6 +54,8 @@ const ( MachineCreationFailed = "MachineCreationFailed" ) +var MachineActuator *Actuator + // Actuator is the AWS-specific actuator for the Cluster API machine controller type Actuator struct { kubeClient kubernetes.Interface @@ -88,6 +90,17 @@ func NewActuator(params ActuatorParams) (*Actuator, error) { func (a *Actuator) Create(cluster *clusterv1.Cluster, machine *clusterv1.Machine) error { mLog := clustoplog.WithMachine(a.logger, machine) mLog.Info("creating machine") + + alreadyExists, err := a.Exists(cluster, machine) + if err != nil { + mLog.Errorf("error getting running instances: %v", err) + return err + } + if alreadyExists { + mLog.Debug("skipped creating a machine that already exists") + return nil + } + instance, err := a.CreateMachine(cluster, machine) if err != nil { mLog.Errorf("error creating machine: %v", err) @@ -218,7 +231,7 @@ func getSubnetIDs(subnet providerconfigv1.AWSResourceReference, client awsclient func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.Machine) (*ec2.Instance, error) { mLog := clustoplog.WithMachine(a.logger, machine) - machineProviderConfig, err := machineProviderFromProviderConfig(machine.Spec.ProviderConfig) + machineProviderConfig, err := ProviderConfigFromMachine(a.codec, machine) if err != nil { mLog.Errorf("error decoding MachineProviderConfig: %v", err) return nil, err @@ -399,7 +412,7 @@ func (a *Actuator) Delete(cluster *clusterv1.Cluster, machine *clusterv1.Machine func (a *Actuator) DeleteMachine(cluster *clusterv1.Cluster, machine *clusterv1.Machine) error { mLog := clustoplog.WithMachine(a.logger, machine) - machineProviderConfig, err := machineProviderFromProviderConfig(machine.Spec.ProviderConfig) + machineProviderConfig, err := ProviderConfigFromMachine(a.codec, machine) if err != nil { mLog.Errorf("error decoding MachineProviderConfig: %v", err) return err @@ -436,7 +449,7 @@ func (a *Actuator) Update(cluster *clusterv1.Cluster, machine *clusterv1.Machine mLog := clustoplog.WithMachine(a.logger, machine) mLog.Debugf("updating machine") - machineProviderConfig, err := machineProviderFromProviderConfig(machine.Spec.ProviderConfig) + machineProviderConfig, err := ProviderConfigFromMachine(a.codec, machine) if err != nil { mLog.Errorf("error decoding MachineProviderConfig: %v", err) return err @@ -534,7 +547,7 @@ func (a *Actuator) Describe(cluster *clusterv1.Cluster, machine *clusterv1.Machi func (a *Actuator) getMachineInstances(cluster *clusterv1.Cluster, machine *clusterv1.Machine) ([]*ec2.Instance, error) { mLog := clustoplog.WithMachine(a.logger, machine) - machineProviderConfig, err := machineProviderFromProviderConfig(machine.Spec.ProviderConfig) + machineProviderConfig, err := ProviderConfigFromMachine(a.codec, machine) if err != nil { mLog.Errorf("error decoding MachineProviderConfig: %v", err) return nil, err diff --git a/pkg/cloud/aws/actuators/machine/config.go b/pkg/cloud/aws/actuators/machine/config.go index 2256674bad..803b7fff0b 100644 --- a/pkg/cloud/aws/actuators/machine/config.go +++ b/pkg/cloud/aws/actuators/machine/config.go @@ -41,7 +41,6 @@ func (a *Actuator) updateStatus(machine *clusterv1.Machine, instance *ec2.Instan if instance == nil { awsStatus.InstanceID = nil awsStatus.InstanceState = nil - mLog.Debugf("WOOT Instance is nil %v", awsStatus) } else { awsStatus.InstanceID = instance.InstanceId awsStatus.InstanceState = instance.State.Name @@ -69,14 +68,8 @@ func (a *Actuator) updateStatus(machine *clusterv1.Machine, instance *ec2.Instan Address: *instance.PrivateDnsName, }) } - mLog.Debugf("WOOT is not nil %+v", awsStatus) - mLog.Debugf("WOOT is not nil InstanceID %v", awsStatus.InstanceID) - mLog.Debugf("WOOT is not nil InstanceState %v", awsStatus.InstanceState) - mLog.Debugf("WOOT is not nil instance %v", instance) } mLog.Debug("finished calculating AWS status") - - mLog.Debugf("WOOT %v", awsStatus.Conditions) awsStatus.Conditions = SetAWSMachineProviderCondition(awsStatus.Conditions, providerconfigv1.MachineCreation, corev1.ConditionTrue, MachineCreationSucceeded, "machine successfully created", UpdateConditionIfReasonOrMessageChange) // TODO(jchaloup): do we really need to update tis? @@ -85,14 +78,11 @@ func (a *Actuator) updateStatus(machine *clusterv1.Machine, instance *ec2.Instan // mLog.Debug("AWS instance ID changed, clearing LastELBSync to trigger adding to ELBs") // awsStatus.LastELBSync = nil // } - mLog.Debugf("AFTER CONDITIONS %v", awsStatus) - mLog.Debugf("AFTER CONDITIONS %v", awsStatus.Conditions) err = a.updateMachineStatus(machine, awsStatus, mLog, networkAddresses) if err != nil { return err } - mLog.Debugf("AFTER Update machine Status") // If machine state is still pending, we will return an error to keep the controllers // attempting to update status until it hits a more permanent state. This will ensure // we get a public IP populated more quickly. @@ -100,7 +90,6 @@ func (a *Actuator) updateStatus(machine *clusterv1.Machine, instance *ec2.Instan mLog.Infof("instance state still pending, returning an error to requeue") return &clustererror.RequeueAfterError{RequeueAfter: requeueAfterSeconds * time.Second} } - mLog.Debugf("END Update status") return nil } @@ -110,7 +99,6 @@ func (a *Actuator) updateMachineStatus(machine *clusterv1.Machine, awsStatus *pr mLog.Errorf("error encoding AWS provider status: %v", err) return err } - mLog.Info("AFTER ENCODING INTO RAW") machineCopy := machine.DeepCopy() if machineCopy.Status.ProviderStatus == nil { @@ -121,13 +109,12 @@ func (a *Actuator) updateMachineStatus(machine *clusterv1.Machine, awsStatus *pr machineCopy.Status.Addresses = networkAddresses } - mLog.Info("AFTER COPYING") if !equality.Semantic.DeepEqual(machine.Status, machineCopy.Status) { mLog.Info("machine status has changed, updating") time := metav1.Now() machineCopy.Status.LastUpdated = &time - _, err := a.clusterClient.ClusterV1alpha1().Machines(machineCopy.Namespace).UpdateStatus(machineCopy) + _, err := a.clusterClient.ClusterV1alpha1().Machines(machineCopy.Namespace).Update(machineCopy) if err != nil { mLog.Errorf("error updating machine status: %v", err) return err @@ -144,9 +131,12 @@ func EncodeProviderStatus(codec codec, awsStatus *providerconfigv1.AWSMachinePro return codec.EncodeProviderStatus(awsStatus) } -func machineProviderFromProviderConfig(providerConfig clusterv1.ProviderConfig) (*providerconfigv1.AWSMachineProviderConfig, error) { +func ProviderConfigFromMachine(codec codec, machine *clusterv1.Machine) (*providerconfigv1.AWSMachineProviderConfig, error) { + //machineProviderCfg := &providerconfigv1.AWSMachineProviderConfig{} + //err := codec.DecodeFromProviderConfig(machine.Spec.ProviderConfig, machineProviderCfg) + //return machineProviderCfg, err var config providerconfigv1.AWSMachineProviderConfig - if err := yaml.Unmarshal(providerConfig.Value.Raw, &config); err != nil { + if err := yaml.Unmarshal(machine.Spec.ProviderConfig.Value.Raw, &config); err != nil { return nil, err } return &config, nil diff --git a/pkg/controller/add_machine.go b/pkg/controller/add_machine.go index 91879c856b..538ed59e3b 100644 --- a/pkg/controller/add_machine.go +++ b/pkg/controller/add_machine.go @@ -18,70 +18,21 @@ package controller import ( "sigs.k8s.io/cluster-api/pkg/controller/machine" - "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset" - "k8s.io/client-go/kubernetes" "sigs.k8s.io/controller-runtime/pkg/manager" machineactuator "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/actuators/machine" - "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1alpha1" - "github.com/golang/glog" - log "github.com/sirupsen/logrus" - awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/client" - "os" +) + +var ( + logLevel string +) + +const ( + defaultLogLevel = "info" ) func init() { // AddToManagerFuncs is a list of functions to create controllers and add them to a manager. AddToManagerFuncs = append(AddToManagerFuncs, func(m manager.Manager) error { - //// the following line exists to make glog happy, for more information, see: https://github.com/kubernetes/kubernetes/issues/17162 - //flag.CommandLine.Parse([]string{}) - //pflag.Parse() - // - //logs.InitLogs() - //defer logs.FlushLogs() - - config := m.GetConfig() - //client, err := m.GetClient() - //if err != nil { - // glog.Fatalf("Could not create client for talking to the apiserver: %v", err) - //} - - client, err := clientset.NewForConfig(config) - if err != nil { - glog.Fatalf("Could not create client for talking to the apiserver: %v", err) - } - - - kubeClient, err := kubernetes.NewForConfig(config) - if err != nil { - glog.Fatalf("Could not create kubernetes client to talk to the apiserver: %v", err) - } - - log.SetOutput(os.Stdout) - if lvl, err := log.ParseLevel("debug"); err != nil { - log.Panic(err) - } else { - log.SetLevel(lvl) - } - - logger := log.WithField("controller", "awsMachine") - - codec, err := v1alpha1.NewCodec() - if err != nil { - glog.Fatal(err) - } - - params := machineactuator.ActuatorParams{ - ClusterClient: client, - KubeClient: kubeClient, - AwsClientBuilder: awsclient.NewClient, - Logger: logger, - Codec: codec, - } - - actuator, err := machineactuator.NewActuator(params) - if err != nil { - glog.Fatalf("Could not create AWS machine actuator: %v", err) - } - return machine.AddWithActuator(m, actuator) + return machine.AddWithActuator(m, machineactuator.MachineActuator) }) }