Skip to content

Commit

Permalink
add log-level flag and makes create idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
enxebre committed Oct 16, 2018
1 parent b60b344 commit 89f6add
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 129 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
69 changes: 67 additions & 2 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,41 @@ 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"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"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 {
Expand All @@ -52,6 +75,7 @@ func main() {
log.Fatal(err)
}

initActuator(mgr)
// Setup all Controllers
if err := controller.AddToManager(mgr); err != nil {
log.Fatal(err)
Expand All @@ -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)
}
}
38 changes: 0 additions & 38 deletions pkg/apis/awsproviderconfig.k8s.io/v1alpha1/register.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ type AWSMachineProviderConfigList struct {
}

func init() {
SchemeBuilder.Register(&AWSMachineProviderConfig{}, &AWSMachineProviderConfigList{})
SchemeBuilder.Register(&AWSMachineProviderStatus{})
SchemeBuilder.Register(&AWSMachineProviderConfigList{}, &AWSMachineProviderConfig{}, &AWSMachineProviderStatus{})
}

// +genclient
Expand Down
9 changes: 4 additions & 5 deletions pkg/apis/awsproviderconfig/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 17 additions & 4 deletions pkg/cloud/aws/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 6 additions & 16 deletions pkg/cloud/aws/actuators/machine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand All @@ -85,22 +78,18 @@ 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.
if awsStatus.InstanceState != nil && *awsStatus.InstanceState == ec2.InstanceStateNamePending {
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
}

Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 89f6add

Please sign in to comment.