Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Jun 18, 2020
1 parent 0d1670f commit 31f6c28
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 308 deletions.
56 changes: 0 additions & 56 deletions aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,6 @@ func getIdToCLBs(svc elbiface.ELBAPI, ids []string) (map[string][]string, error)
return idToCLBs, nil
}

func getIdToASGs(svc autoscalingiface.AutoScalingAPI, ids []string) (map[string][]string, error) {
if len(ids) == 0 {
return nil, nil
}

ec2input := &autoscaling.DescribeAutoScalingInstancesInput{
InstanceIds: aws.StringSlice(ids),
}
nodesResult, err := svc.DescribeAutoScalingInstances(ec2input)
if err != nil {
return nil, fmt.Errorf("Unable to get description for node %v: %v", ids, err)
}
if len(nodesResult.AutoScalingInstances) < 1 {
return nil, fmt.Errorf("Did not get any autoscaling instances for %v", ids)
}

idToASGs := map[string][]string{}

for _, i := range nodesResult.AutoScalingInstances {
idToASGs[*i.InstanceId] = append(idToASGs[*i.InstanceId], *i.AutoScalingGroupName)
}

return idToASGs, nil
}

func getIdToTGs(svc elbv2iface.ELBV2API, ids []string) (map[string][]string, map[string]map[string][]elbv2.TargetDescription, error) {
if len(ids) == 0 {
return nil, nil, nil
Expand Down Expand Up @@ -136,37 +111,6 @@ func getIdToTGs(svc elbv2iface.ELBV2API, ids []string) (map[string][]string, map
return idToTGs, idToTDs, nil
}

func detachInstancesFromASGs(svc autoscalingiface.AutoScalingAPI, asgName string, instanceIDs []string) error {
input := &autoscaling.DetachInstancesInput{
AutoScalingGroupName: aws.String(asgName),
InstanceIds: aws.StringSlice(instanceIDs),
// On manual drain we should probably keep the desired capacity unchanged(hence this should be set to `false`),
// but for automated drains like done by Cluster Autoscaler, we should decrement it as the number of desired instances is managed by CA
//
// We opts to let admins handle manual drain cases on their own.
ShouldDecrementDesiredCapacity: aws.Bool(true),
}

// See https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_DetachInstances.html for the API spec
_, err := svc.DetachInstances(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {

switch aerr.Code() {
case autoscaling.ErrCodeResourceContentionFault:
return fmt.Errorf("Could not detach instances, any resource is in contention, will try in next loop")
default:
return fmt.Errorf("Unknown aws error when detaching instances: %v", aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
return fmt.Errorf("Unknown non-aws error when terminating old instance: %v", err.Error())
}
}
return nil
}

func registerInstancesToCLBs(svc elbiface.ELBAPI, lbName string, instanceIDs []string) error {
instances := []*elb.Instance{}

Expand Down
68 changes: 0 additions & 68 deletions aws_test.go

This file was deleted.

11 changes: 0 additions & 11 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,3 @@ func (n *NodeAttachments) cacheNodeAttachments(nodes []corev1.Node) error {

return nil
}

type TargetGroupReference struct {
// AccountID is AWS Account's ID
AccountID int
// Region is the AWS region to use
Region string
}

const (
TargetGroupReferenceResourceIDSeparator = "."
)
12 changes: 0 additions & 12 deletions detach.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ import (
"k8s.io/apimachinery/pkg/types"
)

const (
LabelValueAttached = "attached"
LabelValueDetached = "detached"

healthy = "Healthy"
)

// deprecatedDetachUnschedulables runs a set of EC2 instance detachments in the loop to update ASGs to not manage unschedulable K8s nodes
func (n *NodeAttachments) deprecatedDetachUnschedulables() error {
return nil
}

func (n *NodeAttachments) detachNodes(unschedulableNodes []corev1.Node) (bool, error) {
var processed int

Expand Down
100 changes: 0 additions & 100 deletions kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,9 @@ package main

import (
"fmt"
"log"
"os"
"path/filepath"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

type KubernetesService interface {
getUnschedulableNodes() ([]corev1.Node, error)
}

type kubernetesSvc struct {
clientset *kubernetes.Clientset
ignoreDaemonSets bool
deleteLocalData bool
}

func kubeGetClientset() (*kubernetes.Clientset, error) {
config, err := rest.InClusterConfig()
if err != nil {
if err == rest.ErrNotInCluster {
config, err = getKubeOutOfCluster()
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("Error getting kubernetes config from within cluster")
}
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}

return clientset, nil
}

func getKubeOutOfCluster() (*rest.Config, error) {
kubeconfig := os.Getenv("KUBECONFIG")
if kubeconfig == "" {
if home := homeDir(); home != "" {
kubeconfig = filepath.Join(home, ".kube", "config")
} else {
return nil, fmt.Errorf("Not KUBECONFIG provided and no home available")
}
}

// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err.Error())
}
return config, nil
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}

func createK8sService() (KubernetesService, error) {
clientset, err := kubeGetClientset()
if err != nil {
log.Fatalf("Error getting kubernetes connection: %v", err)
}
if clientset == nil {
return nil, nil
}
return &kubernetesSvc{clientset: clientset}, nil
}

func (svc *kubernetesSvc) getUnschedulableNodes() ([]corev1.Node, error) {
if svc.clientset == nil {
return nil, nil
}

nodeList, err := svc.clientset.CoreV1().Nodes().List(v1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("Unexpected error getting kubernetes nodes: %v", err)
}

var unschedulables []corev1.Node

for _, node := range nodeList.Items {
if isUnschedulable(node) {
unschedulables = append(unschedulables, node)
}
}

return unschedulables, nil
}

func getInstanceID(node corev1.Node) (string, error) {
labels := node.GetLabels()

Expand All @@ -111,7 +15,3 @@ func getInstanceID(node corev1.Node) (string, error) {

return instanceID, nil
}

func isUnschedulable(node corev1.Node) bool {
return node.Spec.Unschedulable
}
2 changes: 0 additions & 2 deletions log_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"go.uber.org/zap/zapcore"
)

var DefaultLogLevel = "info"

func stringToZapLogLevel(lvl string) zapcore.Level {
switch lvl {
case "debug":
Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ func main() {
// log: exiting because of error: log: cannot create log: open /tmp/manager.controller-manager-5f7bd48566-mzkgz.unknownuser.log.INFO.20200309-120154.1: no such file or directory
klogFlags := flag.NewFlagSet("klog", flag.ContinueOnError)
klog.InitFlags(klogFlags)
klogFlags.Set("logtostderr", "true")
klogFlags.Parse([]string{})
if err := klogFlags.Set("logtostderr", "true"); err != nil {
panic(err)
}

if err := klogFlags.Parse([]string{}); err != nil {
panic(err)
}

var (
syncPeriod time.Duration
Expand Down
1 change: 1 addition & 0 deletions node_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func randStringRunes(n int) string {

var _ = Context("Inside of a new namespace", func() {
ctx := context.TODO()
_ = SetupTest(ctx)

Describe("when no existing resources exist", func() {

Expand Down
57 changes: 0 additions & 57 deletions util_test.go

This file was deleted.

0 comments on commit 31f6c28

Please sign in to comment.