Skip to content

Commit

Permalink
feat: make aci fail-fast creating pods with lifecycle hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
JACQUES Francois authored and JACQUES Francois committed May 24, 2023
1 parent b4d240d commit 0c4ea4f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/provider/aci.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,10 @@ func (p *ACIProvider) getInitContainers(ctx context.Context, pod *v1.Pod) ([]*az
log.G(ctx).Errorf("azure container instances initcontainers do not support readinessProbe")
return nil, errdefs.InvalidInput("azure container instances initContainers do not support readinessProbe")
}
if hasLifecycleHook(initContainer) {
log.G(ctx).Errorf("azure container instances initcontainers do not support lifecycle hooks")
return nil, errdefs.InvalidInput("azure container instances initContainers do not support lifecycle hooks")
}

newInitContainer := azaciv2.InitContainerDefinition{
Name: &pod.Spec.InitContainers[i].Name,
Expand All @@ -1112,6 +1116,9 @@ func (p *ACIProvider) getContainers(pod *v1.Pod) ([]*azaciv2.Container, error) {
if len(podContainers[c].Command) == 0 && len(podContainers[c].Args) > 0 {
return nil, errdefs.InvalidInput("ACI does not support providing args without specifying the command. Please supply both command and args to the pod spec.")
}
if hasLifecycleHook(podContainers[c]) {
return nil, errdefs.InvalidInput("ACI does not support lifecycle hooks")
}
cmd := p.getCommand(podContainers[c])
ports := make([]*azaciv2.ContainerPort, 0, len(podContainers[c].Ports))
aciContainer := azaciv2.Container{
Expand Down Expand Up @@ -1390,3 +1397,10 @@ func getACIEnvVar(e v1.EnvVar) *azaciv2.EnvironmentVariable {
}
return &envVar
}

func hasLifecycleHook(c v1.Container) bool {
hasHandler := func(l *v1.LifecycleHandler) bool {
return l != nil && (l.HTTPGet != nil || l.Exec != nil || l.TCPSocket != nil)
}
return c.Lifecycle != nil && (hasHandler(c.Lifecycle.PreStop) || hasHandler(c.Lifecycle.PostStart))
}
12 changes: 12 additions & 0 deletions pkg/provider/aci_init_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ func TestCreatePodWithInitContainers(t *testing.T) {
},
expectedError: errdefs.InvalidInput("azure container instances initContainers do not support resources requests"),
},
{
description: "Init Containers with lifecycle hook probe",
initContainers: []v1.Container{
{
Name: "initContainer 01",
Lifecycle: &v1.Lifecycle{
PostStart: &v1.LifecycleHandler{Exec: &v1.ExecAction{}},
},
},
},
expectedError: errdefs.InvalidInput("azure container instances initContainers do not support lifecycle hooks"),
},
}
for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
Expand Down
25 changes: 25 additions & 0 deletions pkg/provider/aci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1950,3 +1950,28 @@ func TestFetchStandardPodsEvents(t *testing.T) {
"Normal cg container event container started 2",
}, broadcastEvents)
}

func TestCreatePodWithLifecycleHooks(t *testing.T) {
podName := "pod-" + uuid.New().String()
podNamespace := "ns-" + uuid.New().String()
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

aciMocks := createNewACIMock()

pod := testsutil.CreatePodObj(podName, podNamespace)
pod.Spec.Containers[0].Lifecycle = &corev1.Lifecycle{
PostStart: &corev1.LifecycleHandler{
Exec: &corev1.ExecAction{},
},
}

provider, err := createTestProvider(aciMocks, NewMockConfigMapLister(mockCtrl),
NewMockSecretLister(mockCtrl), NewMockPodLister(mockCtrl), nil)
if err != nil {
t.Fatal("failed to create the test provider", err)
}

err = provider.CreatePod(context.Background(), pod)
assert.Error(t, err, "ACI does not support lifecycle hooks")
}

0 comments on commit 0c4ea4f

Please sign in to comment.