Skip to content

Commit

Permalink
checkPodStatus to use group set with --group argument passed to ketch…
Browse files Browse the repository at this point in the history
… controller
  • Loading branch information
aleksej-paschenko committed Oct 27, 2021
1 parent b204afb commit 0c2c067
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func main() {
return chart.NewHelmClient(namespace)
},
Now: time.Now,
Group: group,
Recorder: mgr.GetEventRecorderFor("App"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "App")
Expand Down
12 changes: 7 additions & 5 deletions internal/controllers/app_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type AppReconciler struct {
HelmFactoryFn helmFactoryFn
Now timeNowFn
Recorder record.EventRecorder
// Group stands for k8s group of Ketch App CRD.
Group string
}

// timeNowFn knows how to get the current time.
Expand Down Expand Up @@ -224,7 +226,7 @@ func (r *AppReconciler) reconcile(ctx context.Context, app *ketchv1.App, logger
}

// retry until all pods for canary deployment comes to running state.
if err := checkPodStatus(r.Client, app.Name, app.Spec.Deployments[1].Version); err != nil {
if err := checkPodStatus(r.Group, r.Client, app.Name, app.Spec.Deployments[1].Version); err != nil {

if !timeoutExpired(app.Spec.Canary.Started, r.Now()) {
return reconcileResult{
Expand Down Expand Up @@ -280,7 +282,7 @@ func timeoutExpired(t *metav1.Time, now time.Time) bool {
}

// checkPodStatus checks whether all pods for a deployment are running or not.
func checkPodStatus(c client.Client, appName string, depVersion ketchv1.DeploymentVersion) error {
func checkPodStatus(group string, c client.Client, appName string, depVersion ketchv1.DeploymentVersion) error {
if c == nil {
return errors.New("client must be non-nil")
}
Expand All @@ -289,13 +291,13 @@ func checkPodStatus(c client.Client, appName string, depVersion ketchv1.Deployme
return errors.New("invalid app specifications")
}

// podList contains list of Pods matching the specifed labels below
// podList contains list of Pods matching the specified labels below
podList := &v1.PodList{}
listOpts := []client.ListOption{
// The specified labels below matches with the required deployment pods of the app.
client.MatchingLabels(map[string]string{
"theketch.io/app-name": appName,
"theketch.io/app-deployment-version": fmt.Sprintf("%d", depVersion)}),
group + "/app-name": appName,
group + "/app-deployment-version": depVersion.String()}),
}

if err := c.List(context.Background(), podList, listOpts...); err != nil {
Expand Down
65 changes: 61 additions & 4 deletions internal/controllers/app_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"context"
"errors"
"fmt"
"testing"
"time"

Expand All @@ -11,20 +12,19 @@ import (
"helm.sh/helm/v3/pkg/release"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlFake "sigs.k8s.io/controller-runtime/pkg/client/fake"

ketchv1 "github.com/shipa-corp/ketch/internal/api/v1beta1"
"github.com/shipa-corp/ketch/internal/chart"
"github.com/shipa-corp/ketch/internal/templates"
"github.com/shipa-corp/ketch/internal/utils/conversions"
)

func stringRef(s string) *string {
return &s
}

type templateReader struct {
templatesErrors map[string]error
}
Expand Down Expand Up @@ -192,3 +192,60 @@ func TestAppReconciler_Reconcile(t *testing.T) {
}
assert.Equal(t, []string{"app-running"}, helmMock.deleteChartCalled)
}

func Test_checkPodStatus(t *testing.T) {
createPod := func(group, appName, version string, status v1.PodStatus) *v1.Pod {
return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%v-%v", appName, version),
Namespace: "default",
Labels: map[string]string{
group + "/app-name": appName,
group + "/app-deployment-version": version,
},
},
Status: status,
}
}
tests := []struct {
name string
pods []runtime.Object
appName string
depVersion ketchv1.DeploymentVersion
group string
wantErr string
}{
{
name: "pod in Pending state",
appName: "my-app",
depVersion: 5,
group: "theketch.io",
pods: []runtime.Object{
createPod("theketch.io", "my-app", "5", v1.PodStatus{Phase: v1.PodPending}),
},
wantErr: `all pods are not running`,
},
{
name: "pod in Pending state but group doesn't match",
appName: "my-app",
depVersion: 5,
group: "ketch.io",
pods: []runtime.Object{
createPod("theketch.io", "my-app", "5", v1.PodStatus{Phase: v1.PodPending}),
},
wantErr: ``,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cli := ctrlFake.NewClientBuilder().WithScheme(clientgoscheme.Scheme).WithRuntimeObjects(tt.pods...).Build()
err := checkPodStatus(tt.group, cli, tt.appName, tt.depVersion)
if len(tt.wantErr) > 0 {
require.NotNil(t, err)
require.Equal(t, tt.wantErr, err.Error())
return
}
require.Nil(t, err)
})
}
}

0 comments on commit 0c2c067

Please sign in to comment.