Skip to content

Initial k8s util implementation#285

Merged
zippolyte merged 14 commits intomasterfrom
hippo/utils_k8s
Jul 12, 2017
Merged

Initial k8s util implementation#285
zippolyte merged 14 commits intomasterfrom
hippo/utils_k8s

Conversation

@zippolyte
Copy link
Copy Markdown
Contributor

What does this PR do?

Initial k8s util implementation

@hkaj
Copy link
Copy Markdown
Member

hkaj commented Jun 9, 2017

Nice! Could you try using https://github.com/ericchiang/k8s instead? It's way slimmer and should be more than enough for our use case. The interface is very similar so you shouldn't have to change many things.

@masci
Copy link
Copy Markdown
Contributor

masci commented Jun 9, 2017

@zippolyte We're currently using the bloated k8s client in the metadata package, If the lightweight client works as expected, could you open another PR and use the new client for metadata too? (see https://github.com/DataDog/datadog-agent/blob/master/pkg/metadata/kubernetes/kubernetes.go)

conorbranagan added a commit that referenced this pull request Jun 10, 2017
Based on feedback in #285
we can use a smaller library that pulls in significantly fewer
dependencies. The only trade-off is that we need to use getters in
function calls to avoid pointers everywhere because this is using older
style protobuf generated code.
Comment thread pkg/util/k8s/k8s.go Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This method queries the API server for the list of pods (see here). There may be a use case for it, but I think we should avoid doing this by default, otherwise we'll put some heavy load on the apiserver.

Maybe rename GetPodList to GetGlobalPodList, and add to the comment that this hits the apiserver so we should use it with caution?

For GetNodeInfo we likely don't need to use this method. We can connect to the local kubelet directly (by making sure users give the agent access to it in the config - we also have some dark magic here in case they don't, ping me if you need more details) and request /pods/ (maybe create a GetLocalPodList that does that)?. This gives you a list of locally running pods.

Once you have that list, you can pick whichever pod you want and extract spec.nodeName and spec.hostIP (just make sure the pod doesn't have spec.hostNetwork set to true or you won't find an IP address). So this gives you the same result but by only querying the local kubelet instead of the global store.

@masci
Copy link
Copy Markdown
Contributor

masci commented Jun 12, 2017

we merged #291 you might want to rebase there to resolve conflicts on glide yaml and lock file

Copy link
Copy Markdown
Member

@hkaj hkaj left a comment

Choose a reason for hiding this comment

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

that looks good to me, although I would like to test it. Let's build a test package where it's used

Comment thread pkg/util/k8s/k8s.go Outdated
}

// PerformKubeletQuery performs a GET query against kubelet and return the response body
// Supports auth
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

token-based auth. TLS is a TODO

Copy link
Copy Markdown
Contributor

@masci masci left a comment

Choose a reason for hiding this comment

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

A couple of things to fix, then let's think about how could we test this (integration or system)

Comment thread pkg/util/k8s/k8s.go Outdated

// Try and find the hostname to query the kubelet
func locateKubelet(kubeletHost string, kubeletPort int) (string, error) {
host := os.Getenv("KUBERNETES_KUBELET_HOST")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's move this in a config option, we can always set it from the outside like DD_ KUBERNETES_KUBELET_HOST, this way seems more consistent to me

Comment thread pkg/util/k8s/k8s.go Outdated
host := os.Getenv("KUBERNETES_KUBELET_HOST")
var err error
if host == "" {
host = kubeletHost
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure an env var should take precedence over a function param.
Question: why NewKubeUtil takes the host as a param? If we want the caller to be able to set it, we should remove any local override. On the other end, if we want the caller to be agnostic of the host, let's remove the param from NewKubeUtil and access KUBERNETES_KUBELET_HOST from the settings (see previous comment) before passing it to locateKubelet

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was trying to replicate the order of precedence that we had in the kube_util of agent 5, where we would pass an instance to this function that could have kubelet_host and kubelet_port set in it, and the env var would take precedence.
My idea was that the check using this would pass whatever was in its config to the function, and let the utility do its thing.
But yeah I agree this isn't really intuitive, and a config option seems better

Comment thread pkg/util/k8s/k8s.go Outdated
}
}

port := kubeletPort
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd move this to a setting as well

Comment thread pkg/util/k8s/k8s.go

// PerformKubeletQuery performs a GET query against kubelet and return the response body
// Supports auth
func PerformKubeletQuery(url string) ([]byte, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure the module should export this func

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We'll need to run Kubelet queries from other places as well, so either we export this method and build the URL where it's needed, or we don't export it and all the queries we need to run will be triggered by new functions we'll add here as needed. Honestly not sure what's the best solution here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we should do both. I think concentrating wrapper functions here is part of the goal of the module itself but keeping this function exported might be a feature more than an issue. Let's keep it like this, ignore my comment.

Copy link
Copy Markdown
Contributor

@masci masci left a comment

Choose a reason for hiding this comment

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

One last comment and we're there!

Comment thread pkg/util/k8s/k8s.go Outdated
// Kubelet constants
const (
AuthTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
DefaultHTTPKubeletPort = 10255
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could we set default port and host settings in init like we do for example here?

config.Datadog.SetDefault("metadata_interval", 4*time.Hour)

@conorbranagan
Copy link
Copy Markdown
Contributor

Curious if we want consistency on kubernetes vs k8s? Right now the meta provider is using kubernetes while this uses k8s. I imagine it's ok to use interchangeably with commenting and such but it might be nice to have consistent pkg names.

@masci
Copy link
Copy Markdown
Contributor

masci commented Jun 19, 2017

I think we should consolidate package and settings names to kubernetes, leaving k8s to informal communications, code comments and stuff like that

@masci
Copy link
Copy Markdown
Contributor

masci commented Jul 9, 2017

We need integration tests for kubernetes, useful both for the util package and the k8s metadata collector. Let's merge this and do that in another PR.

@zippolyte zippolyte merged commit c093cf5 into master Jul 12, 2017
@zippolyte zippolyte deleted the hippo/utils_k8s branch July 12, 2017 13:12
s-alad pushed a commit that referenced this pull request Jan 6, 2026
Bumps [golang.org/x/oauth2](https://github.com/golang/oauth2) from 0.33.0 to 0.34.0.
- [Commits](golang/oauth2@v0.33.0...v0.34.0)

---
updated-dependencies:
- dependency-name: golang.org/x/oauth2
  dependency-version: 0.34.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

4 participants