-
Notifications
You must be signed in to change notification settings - Fork 980
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 pod metrics controller #744
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package metrics | ||
|
||
import ( | ||
"github.com/awslabs/karpenter/pkg/apis/provisioning/v1alpha5" | ||
"github.com/awslabs/karpenter/pkg/metrics" | ||
"github.com/prometheus/client_golang/prometheus" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
controllerName = "Metrics" | ||
|
||
metricSubsystemCapacity = "capacity" | ||
metricSubsystemPods = "pods" | ||
|
||
metricLabelArch = "arch" | ||
metricLabelInstanceType = "instancetype" | ||
metricLabelOS = "os" | ||
metricLabelPhase = "phase" | ||
metricLabelProvisioner = metrics.ProvisionerLabel | ||
metricLabelZone = "zone" | ||
|
||
nodeLabelArch = v1.LabelArchStable | ||
nodeLabelInstanceType = v1.LabelInstanceTypeStable | ||
nodeLabelOS = v1.LabelOSStable | ||
nodeLabelZone = v1.LabelTopologyZone | ||
|
||
nodeConditionTypeReady = v1.NodeReady | ||
) | ||
|
||
var nodeLabelProvisioner = v1alpha5.ProvisionerNameLabelKey | ||
|
||
func publishCount(gaugeVec *prometheus.GaugeVec, labels prometheus.Labels, count int) error { | ||
gauge, err := gaugeVec.GetMetricWith(labels) | ||
if err != nil { | ||
return err | ||
} | ||
gauge.Set(float64(count)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package metrics | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/awslabs/karpenter/pkg/apis/provisioning/v1alpha5" | ||
"github.com/awslabs/karpenter/pkg/cloudprovider" | ||
"go.uber.org/multierr" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/client-go/util/workqueue" | ||
"knative.dev/pkg/logging" | ||
controllerruntime "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
) | ||
|
||
type Controller struct { | ||
CloudProvider cloudprovider.CloudProvider | ||
KubeClient client.Client | ||
} | ||
|
||
func NewController(kubeClient client.Client, cloudProvider cloudprovider.CloudProvider) *Controller { | ||
return &Controller{ | ||
CloudProvider: cloudProvider, | ||
KubeClient: kubeClient, | ||
} | ||
} | ||
|
||
func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { | ||
loggerName := fmt.Sprintf("%s.provisioner/%s", strings.ToLower(controllerName), req.Name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider inlining to avoid creation of an extra variable/concept/line. |
||
ctx = logging.WithLogger(ctx, logging.FromContext(ctx).Named(loggerName)) | ||
|
||
// Does the provisioner exist? | ||
provisioner := &v1alpha5.Provisioner{} | ||
if err := c.KubeClient.Get(ctx, req.NamespacedName, provisioner); err != nil { | ||
if !errors.IsNotFound(err) { | ||
// Unable to determine existence of the provisioner, try again later. | ||
return reconcile.Result{}, err | ||
} | ||
|
||
// The provisioner has been deleted. | ||
return reconcile.Result{}, nil | ||
cjerad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// The provisioner does exist, so update counters. | ||
if err := c.updateCounts(ctx, provisioner); err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
|
||
// Schedule the next run. | ||
return reconcile.Result{RequeueAfter: 10 * time.Second}, nil | ||
} | ||
|
||
func (c *Controller) Register(_ context.Context, m manager.Manager) error { | ||
return controllerruntime. | ||
NewControllerManagedBy(m). | ||
Named(controllerName). | ||
For(&v1alpha5.Provisioner{}). | ||
WithOptions(controller.Options{ | ||
MaxConcurrentReconciles: 10, | ||
}). | ||
Complete(c) | ||
} | ||
|
||
func (c *Controller) updateCounts(ctx context.Context, provisioner *v1alpha5.Provisioner) error { | ||
updateCountFuncs := []func(context.Context, *v1alpha5.Provisioner) error{ | ||
c.updateNodeCounts, | ||
c.updatePodCounts, | ||
} | ||
updateCountFuncsLen := len(updateCountFuncs) | ||
errors := make([]error, updateCountFuncsLen) | ||
workqueue.ParallelizeUntil(ctx, updateCountFuncsLen, updateCountFuncsLen, func(index int) { | ||
errors[index] = updateCountFuncs[index](ctx, provisioner) | ||
}) | ||
|
||
return multierr.Combine(errors...) | ||
} | ||
|
||
func (c *Controller) updateNodeCounts(ctx context.Context, provisioner *v1alpha5.Provisioner) error { | ||
instanceTypes, err := c.CloudProvider.GetInstanceTypes(ctx, &provisioner.Spec.Constraints) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
archValues := sets.NewString() | ||
instanceTypeValues := sets.NewString() | ||
osValues := sets.NewString() | ||
zoneValues := sets.NewString() | ||
for _, instanceType := range instanceTypes { | ||
archValues.Insert(instanceType.Architecture()) | ||
instanceTypeValues.Insert(instanceType.Name()) | ||
osValues.Insert(instanceType.OperatingSystems().UnsortedList()...) | ||
zoneValues.Insert(instanceType.Zones().UnsortedList()...) | ||
} | ||
knownValuesForNodeLabels := map[string]sets.String{ | ||
nodeLabelArch: archValues, | ||
nodeLabelInstanceType: instanceTypeValues, | ||
nodeLabelOS: osValues, | ||
nodeLabelZone: zoneValues, | ||
} | ||
|
||
return publishNodeCounts(provisioner.Name, knownValuesForNodeLabels, func(matchingLabels client.MatchingLabels, consume nodeListConsumerFunc) error { | ||
nodes := v1.NodeList{} | ||
if err := c.KubeClient.List(ctx, &nodes, matchingLabels); err != nil { | ||
return err | ||
} | ||
return consume(nodes.Items) | ||
}) | ||
} | ||
|
||
func (c *Controller) updatePodCounts(ctx context.Context, provisioner *v1alpha5.Provisioner) error { | ||
podsForProvisioner, err := c.podsForProvisioner(ctx, provisioner) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return publishPodCounts(provisioner.Name, podsForProvisioner) | ||
} | ||
|
||
// podsForProvisioner returns a map of slices containing all pods scheduled to nodes in each zone. | ||
func (c *Controller) podsForProvisioner(ctx context.Context, provisioner *v1alpha5.Provisioner) ([]v1.Pod, error) { | ||
// Karpenter does not apply a label, or other marker, to pods. | ||
|
||
results := []v1.Pod{} | ||
|
||
// 1. Fetch all nodes associated with the provisioner. | ||
nodeList := v1.NodeList{} | ||
withProvisionerName := client.MatchingLabels{nodeLabelProvisioner: provisioner.Name} | ||
if err := c.KubeClient.List(ctx, &nodeList, withProvisionerName); err != nil { | ||
return nil, err | ||
} | ||
|
||
// 2. Get all the pods scheduled to each node. | ||
for _, node := range nodeList.Items { | ||
podList := v1.PodList{} | ||
withNodeName := client.MatchingFields{"spec.nodeName": node.Name} | ||
if err := c.KubeClient.List(ctx, &podList, withNodeName); err != nil { | ||
return nil, err | ||
} | ||
|
||
results = append(results, podList.Items...) | ||
} | ||
|
||
return results, nil | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional: Given that the labels are deterministic, we could use https://github.com/prometheus/client_golang/blob/v1.11.0/prometheus/counter.go#L256
gaugeVec.With(labels).Set(float64(count))
which would collapse this helper.