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

Add DataImportCron CronJobs Proxy support #2455

Merged
merged 3 commits into from
Nov 3, 2022

Conversation

arnongilboa
Copy link
Collaborator

@arnongilboa arnongilboa commented Oct 30, 2022

Signed-off-by: Arnon Gilboa [email protected]

What this PR does / why we need it:
Use proxy certificates when creating DataImportCron CronJobs, so the pods won't fail on certificate issues when proxy is configured. cdi-source-update-poller was also fixed to use the proxy certificates.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

Release note:

Add DataImportCron CronJobs Proxy support

@kubevirt-bot kubevirt-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. labels Oct 30, 2022
@kubevirt-bot kubevirt-bot requested review from awels and maya-r October 30, 2022 14:47
Copy link
Collaborator

@akalenyu akalenyu left a comment

Choose a reason for hiding this comment

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

I think we will need to backport this, and I am pretty sure this is backport friendly, so that's awesome

}
container.VolumeMounts = append(container.VolumeMounts, vm)
volumes = append(volumes, createConfigMapVolume(ProxyCertVolName, volName))
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

requeue on err?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

why? nobody said you must have any of the Proxy params set. Similar logic to to the import controller.

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

addEnvVarFromImportProxyConfig := func(varName string) {
if value, err := GetImportProxyConfig(cdiConfig, varName); err == nil {
addEnvVar(varName, value)
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

okay so we knowingly throw away the err from GetImportProxyConfig, just double checking we're okay with that

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

same answer as above.

digest, err := importer.GetImageDigest(url, accessKey, secretKey, certDir, insecureTLS)
allCertDir, err := importer.CreateCertificateDir(certDir)
if err != nil {
os.Exit(1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe log the err so we can debug these (couple more of these down the file)
I see we already use log.Fatalf which prints + os.Exits in here so should be fine?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, adding.

@@ -58,6 +64,8 @@ func main() {
if err != nil {
log.Fatalf("Failed BuildConfigFromFlags, kubeURL %s configPath %s: %v", kubeURL, configPath, err)
}
cfg.TLSClientConfig = rest.TLSClientConfig{Insecure: true}
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm which change in your PR now requires this?
this is just about the cdi client that we use to get stuff (not the polling), why do we have to go insecure?

Copy link
Collaborator Author

@arnongilboa arnongilboa Oct 30, 2022

Choose a reason for hiding this comment

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

The CronJob containerized poller app runs in CDI namespace, and it tries to Get/Update() DataImportCron which is in another namespace, so when proxy is configured, Get/Update() fail on certificate issues unless we configure TLS appropriately. Maybe Insecure: true is not the best idea here, but I have no idea how to use the certs dir for setting TLSClientConfig. wdyt?

Copy link
Collaborator

@akalenyu akalenyu Oct 30, 2022

Choose a reason for hiding this comment

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

AFAIK, under the hood, it should respect proxy env vars so not sure what goes wrong

// Proxy is the proxy func to be used for all requests made by this
// transport. If Proxy is nil, http.ProxyFromEnvironment is used. If Proxy
// returns a nil *URL, no proxy is used.
//
// socks5 proxying does not currently support spdy streaming endpoints.
Proxy func(*http.Request) (*url.URL, error)
Maybe need to append to TLSClientConfig instance ourselves?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, but what would you append?

Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm if it was erroring on x509 I think
https://github.com/kubernetes/apiserver/blob/25ccbfa75c69642e44ac9be141e3bacc7edb3879/pkg/util/webhook/client.go#L136-L140 could work, or something nicer like placing the cert in the golang "respected" path https://go.dev/src/crypto/x509/root_linux.go

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixing that

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice and clean
Just making sure here: the reason we don't care about proxying the k8s API calls is that it's somehow taken care of in another internal k8s layer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We have no good reason to proxy the 2 k8s get/update calls here. Do we?

Copy link
Member

Choose a reason for hiding this comment

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

No the assumption is that the proxy is on the edge of the cluster (aka stuff that tries to go outside of the cluster). I see no use case where it makes sense to proxy traffic inside the cluster.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah reading the openshift proxy docs they back that statement by calling it the cluster-wide egress proxy

@@ -1259,7 +1247,7 @@ func makeImporterContainerSpec(image, verbose, pullPolicy string) *corev1.Contai
}
}

func createProxyConfigMapVolume(certVolName, objRef string) corev1.Volume {
func createConfigMapVolume(certVolName, objRef string) corev1.Volume {
Copy link
Collaborator

Choose a reason for hiding this comment

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

🙏

pkg/controller/dataimportcron-controller.go Show resolved Hide resolved
pkg/controller/dataimportcron-controller.go Show resolved Hide resolved
@arnongilboa arnongilboa force-pushed the dic_cronjob_proxy_support branch from 206c77a to ca4c990 Compare October 31, 2022 11:20
@arnongilboa
Copy link
Collaborator Author

/retest

},
},
TTLSecondsAfterFinished: &ttlSecondsAfterFinished,
BackoffLimit: &backoffLimit,
TTLSecondsAfterFinished: pointer.Int32(3),
Copy link
Member

Choose a reason for hiding this comment

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

So this was 0 before, it is now 3, any reason it changed?

tests/import_proxy_test.go Show resolved Hide resolved
dvName := ""
Eventually(func() string {
cron, err = f.CdiClient.CdiV1beta1().DataImportCrons(ns).Get(context.TODO(), cronName, metav1.GetOptions{})
dvName = cron.Status.CurrentImports[0].DataVolumeName
Copy link
Member

Choose a reason for hiding this comment

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

Can cron.Status.CurrentImports ever have length 0? if so this will not work maybe check to ensure the length > 0 before looking up the name

Also does it make sense to combine these two eventually into one? its looking up the same object just different fields.

tests/import_proxy_test.go Show resolved Hide resolved
@@ -58,6 +64,8 @@ func main() {
if err != nil {
log.Fatalf("Failed BuildConfigFromFlags, kubeURL %s configPath %s: %v", kubeURL, configPath, err)
}
cfg.TLSClientConfig = rest.TLSClientConfig{Insecure: true}
Copy link
Member

Choose a reason for hiding this comment

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

Yes leaving this like this is an immediate CVE, so lets not do that. It looks like TLSClientConfig has a CAData []byte field where you should be able to load the appropriate CA data (from the directory), might have to append the standard CA bundle together with the added CA bundle.

pkg/controller/dataimportcron-controller.go Show resolved Hide resolved
Expect(err).ToNot(HaveOccurred())

By("Checking the importer pod information in the proxy log to verify if the requests were proxied")
verifyImporterPodInfoInProxyLogs(f, url, registryUserAgent, now, BeTrue)
Copy link
Member

Choose a reason for hiding this comment

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

You can't use this function the way it is. This functions looks for the importer pod (which we know works already). We are interested in the poller working properly. So you have to either modify this function to pass in the prefix for the pod to look for, or use some other method of finding the poller pod.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixing

Signed-off-by: Arnon Gilboa <[email protected]>
Copy link
Member

@awels awels left a comment

Choose a reason for hiding this comment

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

looks good, one question and I would love to see some unit tests for the environment variable adding in the dataimportcron-controller

pkg/controller/dataimportcron-controller.go Show resolved Hide resolved
Copy link
Member

@awels awels left a comment

Choose a reason for hiding this comment

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

Three things for the unit test:

  1. You modified the successful history and failed history counts, can you ensure they are what we expect.
  2. You verified the env variables are set when the proxy has a value, can you also verify they are NOT set when the proxy does not have a value?
  3. Also please verify that the extra volume is NOT set when the caProxy value is empty. It is possible to have a proxy but not need a custom CA since the CA of the proxy is signed by a known CA

@arnongilboa arnongilboa force-pushed the dic_cronjob_proxy_support branch from d9f78d0 to 76b56ea Compare November 3, 2022 14:21
Signed-off-by: Arnon Gilboa <[email protected]>
@arnongilboa arnongilboa force-pushed the dic_cronjob_proxy_support branch from 76b56ea to aa983d8 Compare November 3, 2022 15:10
Copy link
Member

@awels awels left a comment

Choose a reason for hiding this comment

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

/approve

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Nov 3, 2022
@kubevirt-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: awels

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

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 3, 2022
@kubevirt-bot kubevirt-bot merged commit eef0358 into kubevirt:main Nov 3, 2022
@awels
Copy link
Member

awels commented Nov 3, 2022

/cherrypick release-v1.55

@kubevirt-bot
Copy link
Contributor

@awels: new pull request created: #2459

In response to this:

/cherrypick release-v1.55

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@kubevirt-bot
Copy link
Contributor

@arnongilboa: new pull request could not be created: failed to create pull request against kubevirt/containerized-data-importer#release-v1.55 from head kubevirt-bot:cherry-pick-2455-to-release-v1.55: status code 422 not one of [201], body: {"message":"Validation Failed","errors":[{"resource":"PullRequest","code":"custom","message":"A pull request already exists for kubevirt-bot:cherry-pick-2455-to-release-v1.55."}],"documentation_url":"https://docs.github.com/rest/reference/pulls#create-a-pull-request"}

In response to this:

/cherrypick release-v1.55
/cherrypick release-v1.49

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@arnongilboa
Copy link
Collaborator Author

/cherrypick release-v1.49

@kubevirt-bot
Copy link
Contributor

@arnongilboa: #2455 failed to apply on top of branch "release-v1.49":

Applying: Add DataImportCron CronJobs Proxy support
Using index info to reconstruct a base tree...
M	pkg/controller/dataimportcron-controller.go
M	pkg/controller/import-controller.go
M	pkg/importer/registry-datasource.go
M	tests/dataimportcron_test.go
M	tests/import_proxy_test.go
M	tests/operator_test.go
M	tools/cdi-source-update-poller/main.go
Falling back to patching base and 3-way merge...
Auto-merging tools/cdi-source-update-poller/main.go
CONFLICT (content): Merge conflict in tools/cdi-source-update-poller/main.go
Auto-merging tests/operator_test.go
Auto-merging tests/import_proxy_test.go
Auto-merging tests/dataimportcron_test.go
Auto-merging pkg/importer/registry-datasource.go
CONFLICT (content): Merge conflict in pkg/importer/registry-datasource.go
Auto-merging pkg/controller/import-controller.go
Auto-merging pkg/controller/dataimportcron-controller.go
CONFLICT (content): Merge conflict in pkg/controller/dataimportcron-controller.go
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Add DataImportCron CronJobs Proxy support
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

In response to this:

/cherrypick release-v1.49

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

arnongilboa added a commit to arnongilboa/containerized-data-importer that referenced this pull request Nov 5, 2022
kubevirt-bot pushed a commit that referenced this pull request Nov 10, 2022
Manual backport of #2455

Signed-off-by: Arnon Gilboa <[email protected]>

Signed-off-by: Arnon Gilboa <[email protected]>
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. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants