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(K8s native sidecar): Add support for Kubernetes native Sidecars #8052

Merged
merged 1 commit into from
Jul 17, 2024

Conversation

kgcarr
Copy link
Contributor

@kgcarr kgcarr commented Jun 13, 2024

This PR is for #7617

Changes

Kubernetes 1.29 introduced native sidecar support

  • Add kubernetes version check as part of the Pod builder
  • Using that check, implemented native sidecar support for versions 1.29 and greater

To implement the native sidecar support, we added an optional RestartPolicy to the sidecar struct. The RestartPolicy will get set if we are using native Kubernetes sidecars and the sidecar container will be added to the initcontainer list instead of the container list.

Need to document that a startupProbe on the sidecar needs to be used for the containers to wait for the sidecar to be ready

Submitter Checklist

As the author of this PR, please check off the items in this checklist:

  • Has Docs if any changes are user facing, including updates to minimum requirements e.g. Kubernetes version bumps
  • Has Tests included if any functionality added or changed
  • pre-commit Passed
  • Follows the commit message standard
  • Meets the Tekton contributor standards (including functionality, content, code)
  • Has a kind label. You can add one by adding a comment on this PR that contains /kind <type>. Valid types are bug, cleanup, design, documentation, feature, flake, misc, question, tep
  • Release notes block below has been updated with any user facing changes (API changes, bug fixes, changes requiring upgrade notices or deprecation warnings). See some examples of good release notes.
  • Release notes contains the string "action required" if the change requires additional action from users switching to the new release

Release Notes

Introducing a feature to adopt Kubernetes-native sidecars, which designates sidecar containers as initContainers. This prevents the need to pull and replace a nop image, leading to faster termination of the sidecars without unnecessary pod errors. Set enable-kubernetes-sidecar to true for Kubernetes 1.29 and later to take advantage of this feature.

@tekton-robot tekton-robot added release-note-none Denotes a PR that doesnt merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 13, 2024
@kgcarr
Copy link
Contributor Author

kgcarr commented Jun 13, 2024

/kind feature

@tekton-robot tekton-robot added the kind/feature Categorizes issue or PR as related to a new feature. label Jun 13, 2024
@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/pod/pod.go 93.9% 34.9% -59.0

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage-df to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/pod/pod.go 93.9% 34.9% -59.0

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/pod/pod.go 93.9% 34.9% -59.0

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage-df to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/pod/pod.go 93.9% 34.9% -59.0

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/pod/pod.go 93.9% 92.8% -1.1
pkg/reconciler/taskrun/taskrun.go 87.3% 82.4% -4.9

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage-df to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/pod/pod.go 93.9% 92.8% -1.1
pkg/reconciler/taskrun/taskrun.go 87.3% 82.4% -4.9

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 92.8% -1.1
pkg/reconciler/taskrun/taskrun.go 87.3% 82.4% -4.9

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage-df to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 92.8% -1.1
pkg/reconciler/taskrun/taskrun.go 87.3% 82.4% -4.9

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 91.5% -2.4
pkg/reconciler/taskrun/taskrun.go 87.3% 82.4% -4.9

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage-df to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 91.5% -2.4
pkg/reconciler/taskrun/taskrun.go 87.3% 82.4% -4.9

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 91.5% -2.4
pkg/reconciler/taskrun/taskrun.go 87.3% 82.4% -4.9

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage-df to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 91.5% -2.4
pkg/reconciler/taskrun/taskrun.go 87.3% 82.4% -4.9

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 91.5% -2.4
pkg/reconciler/taskrun/taskrun.go 87.3% 82.4% -4.9

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage-df to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 91.5% -2.4
pkg/reconciler/taskrun/taskrun.go 87.3% 82.4% -4.9

@pritidesai
Copy link
Member

pritidesai commented Jun 14, 2024

Thanks @kgcarr for working on this.

@tektoncd/core-maintainers, this PR is changing the way sidecars are implemented. With this PR, the sidecar containers are added as initcontainers with necessary specifications to align with k8s native sidecars. K8S introduced native sidecars in 1.29. Tekton CI is running on K8S 1.28. What is the best way to test these changes? Should we add an example or e2e test to run on k8s 1.29 for the changes introduced in this PR? or Should be rely on validations of necessary settings of RestartPolicy for now?

@chitrangpatel
Copy link
Contributor

Thanks @kgcarr for working on this.

@tektoncd/core-maintainers, this PR is changing the way sidecars are implemented. With this PR, the sidecar containers are added as initcontainers with necessary specifications to align with k8s native sidecars. K8S introduced native sidecars in 1.29. Tekton CI is running on K8S 1.28. What is the best way to test these changes? Should we add an example or e2e test to run on k8s 1.29 for the changes introduced in this PR? or Should be rely on validations of necessary settings of RestartPolicy for now?

Should we upgrade the dogfooding cluster to 1.29 so that we can have e2e tests for this?

@chitrangpatel
Copy link
Contributor

Thanks for the PR @kgcarr

With this change, does it mean that we won't have to deal with nop images and infinitely running sidecars until the Taskrun times out?

@kgcarr
Copy link
Contributor Author

kgcarr commented Jun 15, 2024

@chitrangpatel That's correct, we no longer will need the nop substitution for the sidecars. The k8s native sidecar is in the initContainer list so it does not stop the pod from shutting down after the last pod in the Container list finishes.

@afrittoli
Copy link
Member

Thanks @kgcarr for working on this.
@tektoncd/core-maintainers, this PR is changing the way sidecars are implemented. With this PR, the sidecar containers are added as initcontainers with necessary specifications to align with k8s native sidecars. K8S introduced native sidecars in 1.29. Tekton CI is running on K8S 1.28. What is the best way to test these changes? Should we add an example or e2e test to run on k8s 1.29 for the changes introduced in this PR? or Should be rely on validations of necessary settings of RestartPolicy for now?

Should we upgrade the dogfooding cluster to 1.29 so that we can have e2e tests for this?

@chitrangpatel Rather than relying on the version of the dogfooding cluster, since we run kind based tests we can control the k8s version. We have integration, integration-alpha and integration-beta so perhaps we could switch one of the three jobs to 1.29 and test this feature there?

@chitrangpatel
Copy link
Contributor

@chitrangpatel Rather than relying on the version of the dogfooding cluster, since we run kind based tests we can control the k8s version. We have integration, integration-alpha and integration-beta so perhaps we could switch one of the three jobs to 1.29 and test this feature there?

Yes, you're right ofcourse! We have a setup-kind thing that does this.

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage-df to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 91.8% -2.1
pkg/reconciler/taskrun/taskrun.go 87.3% 82.0% -5.3

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/pipeline/v1/container_types.go 62.5% 61.5% -1.0
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 91.8% -2.1
pkg/reconciler/taskrun/taskrun.go 87.3% 82.0% -5.3

pkg/pod/pod.go Outdated
sv, err := dc.ServerVersion()
if err != nil {
return nil, err
}
Copy link
Member

@pritidesai pritidesai Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not very familiar with the DiscoveryInterface(). I tried to find more information about it but could not determine whether it results in an additional API call. Since we have a feature flag for this functionality. I recommend placing it under the if condition that checks the feature flag.

	if config.FromContextOrDefaults(ctx).FeatureFlags.EnableKubernetesSidecar {
		// Kubernetes Version
		sv, err := b.KubeClient.Discovery().ServerVersion()
		if err != nil {
			return nil, err
		}
		// Check if current k8s version is less than 1.29
		// Since Kubernetes Major version cannot be 0 and if it's 2 then sidecar will be in
		// we are only concerned about major version 1 and if the minor is less than 29 then
		// we need to do the current logic
		svMinorInt, _ := strconv.Atoi(sv.Minor)
		svMajorInt, _ := strconv.Atoi(sv.Major)
		if svMajorInt >= 1 && svMinorInt >= SidecarK8sMinorVersionCheck {
			// Add RestartPolicy and Merge into initContainer
			for i := range sidecarContainers {
				sc := &sidecarContainers[i]
				always := corev1.ContainerRestartPolicyAlways
				sc.RestartPolicy = &always
				sc.Name = names.SimpleNameGenerator.RestrictLength(fmt.Sprintf("%v%v", sidecarPrefix, sc.Name))
				mergedPodInitContainers = append(mergedPodInitContainers, *sc)
			}
		}
	} else {
		// Merge sidecar containers with step containers.
		for _, sc := range sidecarContainers {
			sc.Name = names.SimpleNameGenerator.RestrictLength(fmt.Sprintf("%v%v", sidecarPrefix, sc.Name))
			mergedPodContainers = append(mergedPodContainers, sc)
		}
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did slightly different logic to handle the case where you set the enable-kubernetes-sidecar to 'true' but you're running on a kubernetes version less than 1.29

svMinorInt, _ := strconv.Atoi(sv.Minor)
if config.FromContextOrDefaults(ctx).FeatureFlags.EnableKubernetesSidecar && svMajorInt >= 1 && svMinorInt >= 29 {
logger.Infof("Using Kubernetes Native Sidecars \n")
} else {
Copy link
Member

@pritidesai pritidesai Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A similar recommendation to the one above: https://github.com/tektoncd/pipeline/pull/8052/files#r1676425921

go.mod Outdated
@@ -1,6 +1,7 @@
module github.com/tektoncd/pipeline

go 1.22
go 1.22.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: I think we typically avoid changing the Go version in the same PR as introducing a new feature.

@tekton-robot tekton-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesnt merit a release note. labels Jul 12, 2024
@pritidesai
Copy link
Member

Thanks, @kgcarr, for this PR. I appreciate the support from @chitrangpatel, @vdemeester, and @afrittoli as well.

I have completed my review of this PR. From my understanding, these changes should not affect anyone unless the feature flag is enabled and Kubernetes 1.29 is deployed. Once we implement this feature in our deployment, we will report back on any performance improvements and errors, if they arise.

@kgcarr I would like to follow up to ensure we maintain the current code coverage at the very least. See: #8052 (comment)

/approve

@tekton-robot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: pritidesai

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 12, 2024
@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage-df to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/config/feature_flags.go 95.0% 95.1% 0.1
pkg/apis/pipeline/v1/container_types.go 62.5% 62.6% 0.1
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 93.9% 0.0
pkg/pod/status.go 92.2% 92.0% -0.2
pkg/reconciler/taskrun/taskrun.go 87.3% 85.8% -1.5

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/config/feature_flags.go 95.0% 95.1% 0.1
pkg/apis/pipeline/v1/container_types.go 62.5% 62.6% 0.1
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 93.9% 0.0
pkg/pod/status.go 92.2% 92.0% -0.2
pkg/reconciler/taskrun/taskrun.go 87.3% 85.8% -1.5

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage-df to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/config/feature_flags.go 95.0% 95.1% 0.1
pkg/apis/pipeline/v1/container_types.go 62.5% 62.6% 0.1
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 93.9% 0.0
pkg/pod/status.go 92.2% 92.0% -0.2
pkg/reconciler/taskrun/taskrun.go 87.3% 85.8% -1.5

@tekton-robot
Copy link
Collaborator

The following is the coverage report on the affected files.
Say /test pull-tekton-pipeline-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/apis/config/feature_flags.go 95.0% 95.1% 0.1
pkg/apis/pipeline/v1/container_types.go 62.5% 62.6% 0.1
pkg/apis/pipeline/v1beta1/container_types.go 36.2% 36.1% -0.1
pkg/pod/pod.go 93.9% 93.9% 0.0
pkg/pod/status.go 92.2% 92.0% -0.2
pkg/reconciler/taskrun/taskrun.go 87.3% 85.8% -1.5

@kgcarr
Copy link
Contributor Author

kgcarr commented Jul 13, 2024

/retest

@@ -9,6 +9,7 @@ spec:
image: docker.io/library/ubuntu
script: |
echo "hello from sidecar" > /shared/message
sleep 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤞 Hope this is not flaky.

if svMajorInt >= 1 && svMinorInt >= 29 {
useTektonSidecar = false
logger.Infof("Using Kubernetes Native Sidecars \n")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if clause needs Test coverage.

for _, s := range pod.Status.InitContainerStatuses {
if IsContainerSidecar(s.Name) {
sidecarStatuses = append(sidecarStatuses, s)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs test

Copy link
Contributor

@chitrangpatel chitrangpatel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm Thanks @kgcarr

@chitrangpatel
Copy link
Contributor

/lgtm

@tekton-robot tekton-robot added the lgtm Indicates that a PR is ready to be merged. label Jul 16, 2024
@chitrangpatel
Copy link
Contributor

/retest

@pritidesai
Copy link
Member

Integration tests failing with 😿 :

docker: Error response from daemon: toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit.

@pritidesai
Copy link
Member

/retest

1 similar comment
@chitrangpatel
Copy link
Contributor

/retest

@tekton-robot tekton-robot merged commit c5ea9cf into tektoncd:main Jul 17, 2024
14 checks passed
@@ -2317,7 +2317,7 @@ status:
// Check actions
actions := clients.Kube.Actions()
if len(actions) != 2 || !actions[0].Matches("list", "configmaps") || !actions[1].Matches("watch", "configmaps") {
t.Errorf("expected 2 actions (list configmaps, and watch configmaps) created by the reconciler,"+
t.Errorf("expected 3 actions (list configmaps, and watch configmaps) created by the reconciler,"+
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to fix this...adding it to #8128

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants