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

feat: make aci fail-fast creating pods with lifecycle hooks #548

Merged
merged 2 commits into from
Jun 5, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}