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

refactor: reduce usage of k8s.io/kubernetes packages #205

Closed
wants to merge 1 commit into from
Closed

refactor: reduce usage of k8s.io/kubernetes packages #205

wants to merge 1 commit into from

Conversation

ash2k
Copy link
Member

@ash2k ash2k commented Jan 10, 2021

Updates #56.

The only usage left is in the diff:

kubescheme.Scheme.Default(patched)

It uses the legacy scheme (via https://github.com/argoproj/gitops-engine/blob/master/pkg/utils/kube/scheme/scheme.go) to perform defaulting, which depends on all the internal k/k schemas. It's fragile (see #56 (comment)) and it does not work for custom resources too. I wonder if there is a better way? Maybe it's possible to use the apply api with dry-run flag (don't know if that's a thing)?

Defaulting using the scheme was introduced in #154. /cc @jgwest

Regardless of the above, I think this PR is mergeable with one last step remaining to fix #56.

@sonarcloud
Copy link

sonarcloud bot commented Jan 10, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
10.1% 10.1% Duplication

@@ -38,7 +38,6 @@ type OnKubectlRunFunc func(command string) (CleanupFunc, error)

type Kubectl interface {
ApplyResource(ctx context.Context, config *rest.Config, obj *unstructured.Unstructured, namespace string, dryRunStrategy cmdutil.DryRunStrategy, force, validate bool) (string, error)
ConvertToVersion(obj *unstructured.Unstructured, group, version string) (*unstructured.Unstructured, error)
Copy link
Member Author

Choose a reason for hiding this comment

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

vX->vY conversion only works on known objects, does not work on CRDs. Also, no need to do it here since the engine is watching all resources anyway - is that right? Or what are the cases when conversion is needed?

@codecov-io
Copy link

codecov-io commented Jan 10, 2021

Codecov Report

Merging #205 (0cb19f8) into master (2a9c144) will increase coverage by 0.73%.
The diff coverage is 33.33%.

❗ Current head 0cb19f8 differs from pull request most recent head 51e6cb2. Consider uploading reports for the commit 51e6cb2 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #205      +/-   ##
==========================================
+ Coverage   49.21%   49.95%   +0.73%     
==========================================
  Files          40       39       -1     
  Lines        3251     3007     -244     
==========================================
- Hits         1600     1502      -98     
+ Misses       1491     1351     -140     
+ Partials      160      154       -6     
Impacted Files Coverage Δ
pkg/cache/cluster.go 55.39% <0.00%> (+3.16%) ⬆️
pkg/diff/diff.go 62.78% <ø> (ø)
pkg/utils/kube/ctl.go 0.00% <ø> (-2.12%) ⬇️
pkg/health/health_pod.go 78.37% <100.00%> (ø)
pkg/cache/settings.go 44.44% <0.00%> (-3.84%) ⬇️
pkg/sync/sync_tasks.go 89.02% <0.00%> (-2.13%) ⬇️
pkg/health/health_hpa.go 82.19% <0.00%> (-0.48%) ⬇️
pkg/sync/sync_context.go 71.48% <0.00%> (-0.12%) ⬇️
pkg/sync/common/types.go 58.82% <0.00%> (ø)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2a9c144...51e6cb2. Read the comment docs.

@ash2k
Copy link
Member Author

ash2k commented Apr 5, 2021

@alexmt =)

@sonarcloud
Copy link

sonarcloud bot commented Apr 17, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
10.4% 10.4% Duplication

Copy link
Contributor

@alexmt alexmt left a comment

Choose a reason for hiding this comment

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

Sorry for ignoring PR for so long.

I agree with the EndPoint related change, however, I don't think we can remove the conversion code just yet. Client-side conversion allows us to reconcile a lot of resources very quickly and don't store multiple versions of the same resource in memory.

@ash2k I understand the desire to get rid of k8s dependencies.

I think the only way do to it is to switch to server-side diffing. The list of K8S CRDs is growing and client-side diffing will be less and less efficient in the future. So we have to switch sooner or later. This would allow us to get rid of k8s/kubernetes dependency.

It is not the top priority for Argo CD yet. How urgent it is work Gitlab k8s agent? We can consider and workaround: switch engine to server-side diffing as a default but make it possible to override diffing and use client-side diffing in Argo CD only.

@@ -597,19 +596,6 @@ func (k *KubectlCmd) authReconcile(ctx context.Context, config *rest.Config, kub
return strings.Join(out, ". "), nil
}

// ConvertToVersion converts an unstructured object into the specified group/version
func (k *KubectlCmd) ConvertToVersion(obj *unstructured.Unstructured, group string, version string) (*unstructured.Unstructured, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

GitOps engine "watch" preferred versions of the resources to minimize memory usage. So we have to rely on conversion to avoid making an API cal and do client-side only diffing. It is critical for performance when the engine is used to manage multiple clusters.

@alexmt
Copy link
Contributor

alexmt commented Apr 19, 2021

Another note:

Client-side diffing is very critical for Argo CD because it has the requirement to manage hundreds of clusters and visualizes real-time diff in the UI. We were planning to "cache" diffing result using probably resourceVersion or generation fields. Caching implementation will take some time. Server side diffing might work perfectly fine for Gitlab Kubernetes Agent.

@ash2k
Copy link
Member Author

ash2k commented Apr 20, 2021

@alexmt Ok, cool. Thanks for the review. I've opened #258.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Drop k8s.io/kubernetes dependency
3 participants