Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use apps/v1 for Deployment for all K8s versions #2238

Merged
merged 2 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 3 additions & 9 deletions pkg/ctl/create/fargate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/fargate"
"github.com/weaveworks/eksctl/pkg/fargate/coredns"
"github.com/weaveworks/eksctl/pkg/utils"
"github.com/weaveworks/eksctl/pkg/utils/retry"
"github.com/weaveworks/eksctl/pkg/utils/strings"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -139,24 +138,19 @@ func doCreateFargateProfiles(cmd *cmdutils.Cmd, ctl *eks.ClusterProvider) error

func scheduleCoreDNSOnFargateIfRelevant(cmd *cmdutils.Cmd, clientSet kubernetes.Interface) error {
if coredns.IsSchedulableOnFargate(cmd.ClusterConfig.FargateProfiles) {
betaAPIDeprecated, err := utils.IsMinVersion(api.Version1_16, cmd.ClusterConfig.Metadata.Version)
if err != nil {
return err
}
useBetaAPIGroup := !betaAPIDeprecated
scheduled, err := coredns.IsScheduledOnFargate(clientSet, useBetaAPIGroup)
scheduled, err := coredns.IsScheduledOnFargate(clientSet)
if err != nil {
return err
}
if !scheduled {
if err := coredns.ScheduleOnFargate(clientSet, useBetaAPIGroup); err != nil {
if err := coredns.ScheduleOnFargate(clientSet); err != nil {
return err
}
retryPolicy := &retry.TimingOutExponentialBackoff{
Timeout: cmd.ProviderConfig.WaitTimeout,
TimeUnit: time.Second,
}
if err := coredns.WaitForScheduleOnFargate(clientSet, retryPolicy, useBetaAPIGroup); err != nil {
if err := coredns.WaitForScheduleOnFargate(clientSet, retryPolicy); err != nil {
return err
}
}
Expand Down
36 changes: 19 additions & 17 deletions pkg/fargate/coredns/coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kubeclient "k8s.io/client-go/kubernetes"
)

Expand Down Expand Up @@ -45,8 +46,8 @@ func selectsCoreDNS(selector api.FargateProfileSelector) bool {
}

// IsScheduledOnFargate checks if EKS' coredns is scheduled onto Fargate.
func IsScheduledOnFargate(clientSet kubeclient.Interface, useBetaAPIGroup bool) (bool, error) {
isDepOnFargate, err := isDeploymentScheduledOnFargate(clientSet, useBetaAPIGroup)
func IsScheduledOnFargate(clientSet kubeclient.Interface) (bool, error) {
isDepOnFargate, err := isDeploymentScheduledOnFargate(clientSet)
if err != nil {
return false, err
}
Expand All @@ -57,19 +58,19 @@ func IsScheduledOnFargate(clientSet kubeclient.Interface, useBetaAPIGroup bool)
return isDepOnFargate && arePodsOnFargate, nil
}

func isDeploymentScheduledOnFargate(clientSet kubeclient.Interface, useBetaAPIGroup bool) (bool, error) {
coredns, err := getDeployment(clientSet, useBetaAPIGroup)
func isDeploymentScheduledOnFargate(clientSet kubeclient.Interface) (bool, error) {
coredns, err := clientSet.AppsV1().Deployments(Namespace).Get(Name, metav1.GetOptions{})
if err != nil {
return false, err
}
if coredns.Replicas() == nil {
if coredns.Spec.Replicas == nil {
return false, errors.New("nil spec.replicas in coredns deployment")
}
computeType, exists := coredns.PodAnnotations()[ComputeTypeAnnotationKey]
logger.Debug("deployment %q with compute type %q currently has %v/%v replicas running", Name, computeType, coredns.ReadyReplicas(), *coredns.Replicas())
computeType, exists := coredns.Spec.Template.Annotations[ComputeTypeAnnotationKey]
logger.Debug("deployment %q with compute type %q currently has %v/%v replicas running", Name, computeType, coredns.Status.ReadyReplicas, *coredns.Spec.Replicas)
scheduled := exists &&
computeType == computeTypeFargate &&
*coredns.Replicas() == coredns.ReadyReplicas()
*coredns.Spec.Replicas == coredns.Status.ReadyReplicas
if scheduled {
logger.Info("%q is now scheduled onto Fargate", Name)
}
Expand Down Expand Up @@ -103,29 +104,30 @@ func isRunningOnFargate(pod *v1.Pod) bool {

// ScheduleOnFargate modifies EKS' coredns deployment so that it can be scheduled
// on Fargate.
func ScheduleOnFargate(clientSet kubeclient.Interface, useBetaAPIGroup bool) error {
if err := scheduleOnFargate(clientSet, useBetaAPIGroup); err != nil {
func ScheduleOnFargate(clientSet kubeclient.Interface) error {
if err := scheduleOnFargate(clientSet); err != nil {
return errors.Wrapf(err, "failed to make %q deployment schedulable on Fargate", Name)
}
logger.Info("%q is now schedulable onto Fargate", Name)
return nil
}

func scheduleOnFargate(clientSet kubeclient.Interface, useBetaAPIGroup bool) error {
coredns, err := getDeployment(clientSet, useBetaAPIGroup)
func scheduleOnFargate(clientSet kubeclient.Interface) error {
deployments := clientSet.AppsV1().Deployments(Namespace)
coredns, err := deployments.Get(Name, metav1.GetOptions{})
if err != nil {
return err
}
coredns.PodAnnotations()[ComputeTypeAnnotationKey] = computeTypeFargate
coredns.Spec.Template.Annotations[ComputeTypeAnnotationKey] = computeTypeFargate
bytes, err := runtime.Encode(unstructured.UnstructuredJSONScheme, coredns)
if err != nil {
return errors.Wrapf(err, "failed to marshal %q deployment", Name)
}
patched, err := patchDeployment(clientSet, useBetaAPIGroup, bytes)
patched, err := deployments.Patch(Name, types.MergePatchType, bytes)
if err != nil {
return errors.Wrap(err, "failed to patch deployment")
}
value, exists := patched.PodAnnotations()[ComputeTypeAnnotationKey]
value, exists := patched.Spec.Template.Annotations[ComputeTypeAnnotationKey]
if !exists {
return fmt.Errorf("could not find annotation %q on patched deployment %q: patching must have failed", ComputeTypeAnnotationKey, Name)
}
Expand All @@ -138,11 +140,11 @@ func scheduleOnFargate(clientSet kubeclient.Interface, useBetaAPIGroup bool) err
// WaitForScheduleOnFargate waits for coredns to be scheduled on Fargate.
// It will wait until it has detected that the scheduling has been successful,
// or until the retry policy times out, whichever happens first.
func WaitForScheduleOnFargate(clientSet kubeclient.Interface, retryPolicy retry.Policy, useBetaAPIGroup bool) error {
func WaitForScheduleOnFargate(clientSet kubeclient.Interface, retryPolicy retry.Policy) error {
// Clone the retry policy to ensure this method is re-entrant/thread-safe:
retryPolicy = retryPolicy.Clone()
for !retryPolicy.Done() {
isScheduled, err := IsScheduledOnFargate(clientSet, useBetaAPIGroup)
isScheduled, err := IsScheduledOnFargate(clientSet)
if err != nil {
return err
}
Expand Down
71 changes: 3 additions & 68 deletions pkg/fargate/coredns/coredns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/fargate/coredns"
Expand All @@ -15,8 +14,6 @@ import (
"github.com/weaveworks/eksctl/pkg/utils/retry"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kubeclient "k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -125,7 +122,7 @@ var _ = Describe("coredns", func() {
Expect(err).To(Not(HaveOccurred()))
Expect(deployment.Spec.Template.Annotations).To(HaveKeyWithValue(coredns.ComputeTypeAnnotationKey, "ec2"))
// When:
err = coredns.ScheduleOnFargate(mockClientset, false)
err = coredns.ScheduleOnFargate(mockClientset)
Expect(err).To(Not(HaveOccurred()))
// Then:
deployment, err = mockClientset.AppsV1().Deployments(coredns.Namespace).Get(coredns.Name, metav1.GetOptions{})
Expand All @@ -141,7 +138,7 @@ var _ = Describe("coredns", func() {
deployment("fargate", 2, 2), pod("fargate", v1.PodRunning), pod("fargate", v1.PodRunning),
)
// When:
err := coredns.WaitForScheduleOnFargate(mockClientset, retryPolicy, false)
err := coredns.WaitForScheduleOnFargate(mockClientset, retryPolicy)
// Then:
Expect(err).To(Not(HaveOccurred()))
})
Expand All @@ -160,50 +157,14 @@ var _ = Describe("coredns", func() {
// Given:
mockClientset := mockClientsetWith(failureCase...)
// When:
err := coredns.WaitForScheduleOnFargate(mockClientset, retryPolicy, false)
err := coredns.WaitForScheduleOnFargate(mockClientset, retryPolicy)
// Then:
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("timed out while waiting for \"coredns\" to be scheduled on Fargate"))
}
})
})

Describe("API Group", func() {
expectError := func(err error) {
Expect(err).To(HaveOccurred())
statusErr, ok := err.(*errors.StatusError)
Expect(ok).To(BeTrue())
Expect(statusErr.Status().Code).To(Equal(int32(404)))
}

DescribeTable("should use the correct API group", func(object runtime.Object, useBetaAPIGroup bool) {
mockClientset := mockClientsetWith(object)
err := coredns.ScheduleOnFargate(mockClientset, useBetaAPIGroup)
Expect(err).ToNot(HaveOccurred())

deployment, err := mockClientset.ExtensionsV1beta1().Deployments(coredns.Namespace).Get(coredns.Name, metav1.GetOptions{})
if !useBetaAPIGroup {
expectError(err)
} else {
Expect(err).ToNot(HaveOccurred())
Expect(deployment.Spec.Template.Annotations[coredns.ComputeTypeAnnotationKey]).To(Equal("fargate"))
}

appsDeployment, err := mockClientset.AppsV1().Deployments(coredns.Namespace).Get(coredns.Name, metav1.GetOptions{})
if useBetaAPIGroup {
expectError(err)
} else {
Expect(err).ToNot(HaveOccurred())
Expect(appsDeployment.Spec.Template.Annotations[coredns.ComputeTypeAnnotationKey]).To(Equal("fargate"))
}

err = coredns.WaitForScheduleOnFargate(mockClientset, retryPolicy, useBetaAPIGroup)
Expect(err).ToNot(HaveOccurred())
},
Entry("apps/v1 Deployment", deployment("ec2", 2, 2), false),
Entry("extensions/v1beta1 Deployment", betaDeployment("ec2", 2, 2), true),
)
})
})

func mockClientsetWith(objects ...runtime.Object) kubeclient.Interface {
Expand Down Expand Up @@ -236,32 +197,6 @@ func deployment(computeType string, numReady, numReplicas int32) *appsv1.Deploym
}
}

func betaDeployment(computeType string, numReady, numReplicas int32) *v1beta1.Deployment {
return &v1beta1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: v1beta1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: coredns.Namespace,
Name: coredns.Name,
},
Spec: v1beta1.DeploymentSpec{
Replicas: &numReplicas,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
coredns.ComputeTypeAnnotationKey: computeType,
},
},
},
},
Status: v1beta1.DeploymentStatus{
ReadyReplicas: numReady,
},
}
}

const chars = "abcdef0123456789"

func pod(computeType string, phase v1.PodPhase) *v1.Pod {
Expand Down
69 changes: 0 additions & 69 deletions pkg/fargate/coredns/deployment.go

This file was deleted.