Skip to content

Commit 4810df7

Browse files
committed
first set of metrics
1 parent f2ce04d commit 4810df7

8 files changed

+96
-9
lines changed

Makefile

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ GOVULNCHECK_VER := v1.0.0
163163
GOVULNCHECK := $(abspath $(TOOLS_BIN_DIR)/$(GOVULNCHECK_BIN)-$(GOVULNCHECK_VER))
164164
GOVULNCHECK_PKG := golang.org/x/vuln/cmd/govulncheck
165165

166+
KUBE_STATE_METRICS_VER := e31ed9ab
167+
KUBE_STATE_METRICS_BIN := kube-state-metrics
168+
KUBE_STATE_METRICS := $(abspath $(TOOLS_BIN_DIR)/$(KUBE_STATE_METRICS_BIN)-$(KUBE_STATE_METRICS_VER))
169+
KUBE_STATE_METRICS_PKG := k8s.io/kube-state-metrics/v2
170+
KUBE_STATE_METRICS_MOD_REPLACE := $(KUBE_STATE_METRICS_PKG)=github.com/chrischdi/kube-state-metrics/v2@$(KUBE_STATE_METRICS_VER)
171+
166172
GOVC_VER := $(shell cat go.mod | grep "github.com/vmware/govmomi" | awk '{print $$NF}')
167173
GOVC_BIN := govc
168174
GOVC := $(abspath $(TOOLS_BIN_DIR)/$(GOVC_BIN)-$(GOVC_VER))
@@ -184,6 +190,8 @@ RELEASE_NOTES_VER := $(CAPI_HACK_TOOLS_VER)
184190
RELEASE_NOTES_BIN := release-notes
185191
RELEASE_NOTES := $(abspath $(TOOLS_BIN_DIR)/$(RELEASE_NOTES_BIN)-$(RELEASE_NOTES_VER))
186192
RELEASE_NOTES_PKG := sigs.k8s.io/cluster-api/hack/tools/release
193+
# let go resolve the tools version via the CAPI version.
194+
RELEASE_NOTES_MOD_REQUIRE := sigs.k8s.io/cluster-api/hack/tools@$(RELEASE_NOTES_VER)
187195

188196
# Define Docker related variables. Releases should modify and double check these vars.
189197
REGISTRY ?= gcr.io/$(shell gcloud config get-value project)
@@ -237,7 +245,7 @@ help: # Display this help
237245

238246
.PHONY: generate
239247
generate: ## Run all generate targets
240-
$(MAKE) generate-modules generate-manifests generate-go-deepcopy generate-go-conversions generate-flavors
248+
$(MAKE) generate-modules generate-manifests generate-go-deepcopy generate-go-conversions generate-flavors generate-metrics-config
241249

242250
.PHONY: generate-manifests
243251
generate-manifests: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc.
@@ -316,6 +324,9 @@ generate-e2e-templates-main: $(KUSTOMIZE) ## Generate test templates for the mai
316324
# for DHCP overrides
317325
"$(KUSTOMIZE)" --load-restrictor LoadRestrictionsNone build $(E2E_TEMPLATE_DIR)/main/dhcp-overrides > $(E2E_TEMPLATE_DIR)/main/cluster-template-dhcp-overrides.yaml
318326

327+
.PHONY: generate-metrics-config
328+
generate-metrics-config: $(KUBE_STATE_METRICS) ## Generate ./crd-metrics-config.yaml
329+
$(KUBE_STATE_METRICS) generate ./apis/v1beta1/... > config/metrics/crd-metrics-config.yaml
319330

320331
## --------------------------------------
321332
## Lint / Verify
@@ -407,7 +418,6 @@ verify-flavors: $(FLAVOR_DIR) generate-flavors ## Verify generated flavors
407418
echo "flavor files in templates directory are out of date"; exit 1; \
408419
fi
409420

410-
411421
## --------------------------------------
412422
## Build
413423
## --------------------------------------
@@ -606,7 +616,6 @@ e2e-flavors-main: $(RELEASE_DIR)
606616
mkdir -p $(RELEASE_DIR)/main
607617
$(MAKE) generate-flavors FLAVOR_DIR=$(RELEASE_DIR)/main
608618

609-
610619
.PHONY: generate-flavors
611620
generate-flavors: $(FLAVOR_DIR)
612621
go run ./packaging/flavorgen --output-dir $(FLAVOR_DIR)
@@ -763,13 +772,15 @@ $(GOLANGCI_LINT_BIN): $(GOLANGCI_LINT) ## Build a local copy of golangci-lint.
763772
.PHONY: $(GOVULNCHECK_BIN)
764773
$(GOVULNCHECK_BIN): $(GOVULNCHECK) ## Build a local copy of govulncheck.
765774

775+
.PHONY: $(KUBE_STATE_METRICS_BIN)
776+
$(KUBE_STATE_METRICS_BIN): $(KUBE_STATE_METRICS) ## Build a local copy of metric-gen.
777+
766778
.PHONY: $(GOVC_BIN)
767779
$(GOVC_BIN): $(GOVC) ## Build a local copy of govc.
768780

769781
.PHONY: $(KIND_BIN)
770782
$(KIND_BIN): $(KIND) ## Build a local copy of kind.
771783

772-
773784
.PHONY: $(RELEASE_NOTES_BIN)
774785
$(RELEASE_NOTES_BIN): $(RELEASE_NOTES) ## Build a local copy of release-notes.
775786

@@ -815,15 +826,17 @@ $(GOLANGCI_LINT): # Build golangci-lint.
815826
$(GOVULNCHECK): # Build govulncheck.
816827
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(GOVULNCHECK_PKG) $(GOVULNCHECK_BIN) $(GOVULNCHECK_VER)
817828

829+
$(KUBE_STATE_METRICS): # Build kube-state-metrics.
830+
GOBIN=$(TOOLS_BIN_DIR) GOMOD_REPLACE="$(KUBE_STATE_METRICS_MOD_REPLACE)" $(GO_TOOLS_BUILD) $(KUBE_STATE_METRICS_PKG) $(KUBE_STATE_METRICS_BIN) $(KUBE_STATE_METRICS_VER)
831+
818832
$(GOVC): # Build GOVC.
819833
CGO_ENABLED=0 GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(GOVC_PKG) $(GOVC_BIN) $(GOVC_VER)
820834

821835
$(KIND): # Build kind.
822836
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(KIND_PKG) $(KIND_BIN) $(KIND_VER)
823837

824-
825838
$(RELEASE_NOTES): # Build release-notes.
826-
GOBIN=$(TOOLS_BIN_DIR) $(GO_TOOLS_BUILD) $(RELEASE_NOTES_PKG) $(RELEASE_NOTES_BIN) $(RELEASE_NOTES_VER)
839+
GOBIN=$(TOOLS_BIN_DIR) GOMOD_REQUIRE="$(RELEASE_NOTES_MOD_REQUIRE)" $(GO_TOOLS_BUILD) $(RELEASE_NOTES_PKG) $(RELEASE_NOTES_BIN) $(RELEASE_NOTES_VER)
827840

828841
## --------------------------------------
829842
## Helpers

apis/v1beta1/vspherecluster_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ type VSphereClusterStatus struct {
9191

9292
// Conditions defines current service state of the VSphereCluster.
9393
// +optional
94+
// +Metrics:stateset:name="status_condition",help="The condition of a vspherecluster.",labelName="status",JSONPath=".status",list={"True","False","Unknown"},labelsFromPath={"type":".type"}
95+
// +Metrics:gauge:name="status_condition_last_transition_time",help="The condition last transition time of a vspherecluster.",valueFrom=.lastTransitionTime,labelsFromPath={"type":".type","status":".status"}
9496
Conditions clusterv1.Conditions `json:"conditions,omitempty"`
9597

9698
// FailureDomains is a list of failure domain objects synced from the infrastructure provider.
@@ -110,6 +112,15 @@ type VSphereClusterStatus struct {
110112
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of Machine"
111113

112114
// VSphereCluster is the Schema for the vsphereclusters API
115+
// +Metrics:gvk:namePrefix="capi_vspherecluster"
116+
// +Metrics:labelFromPath:name="name",JSONPath=".metadata.name"
117+
// +Metrics:labelFromPath:name="namespace",JSONPath=".metadata.namespace"
118+
// +Metrics:labelFromPath:name="uid",JSONPath=".metadata.uid"
119+
// +Metrics:gauge:name="created",JSONPath=".metadata.creationTimestamp",help="Unix creation timestamp."
120+
// +Metrics:info:name="annotation_paused",JSONPath=.metadata.annotations['cluster\.x-k8s\.io/paused'],help="Whether the vspherecluster is paused and any of its resources will not be processed by the controllers.",labelsFromPath={paused_value:"."}
121+
// +Metrics:info:name="owner",JSONPath=".metadata.ownerReferences",help="Owner references.",labelsFromPath={owner_is_controller:".controller",owner_kind:".kind",owner_name:".name",owner_uid:".uid"}
122+
// +Metrics:labelFromPath:name="cluster_name",JSONPath=.metadata.labels.cluster\.x-k8s\.io/cluster-name
123+
// +Metrics:info:name="info",help="Information about a vspherecluster.",labelsFromPath={status_vsphere_version:.status.vCenterVersion,spec_server:.spec.server,identity_reference_kind:".spec.identityRef.kind",identity_reference_name:".spec.identityRef.name"}
113124
type VSphereCluster struct {
114125
metav1.TypeMeta `json:",inline"`
115126
metav1.ObjectMeta `json:"metadata,omitempty"`

apis/v1beta1/vsphereclusteridentity_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type VSphereClusterIdentityStatus struct {
4545

4646
// Conditions defines current service state of the VSphereCluster.
4747
// +optional
48+
// +Metrics:stateset:name="status_condition",help="The condition of a vsphereclusteridentity.",labelName="status",JSONPath=".status",list={"True","False","Unknown"},labelsFromPath={"type":".type"}
49+
// +Metrics:gauge:name="status_condition_last_transition_time",help="The condition last transition time of a vsphereclusteridentity.",valueFrom=.lastTransitionTime,labelsFromPath={"type":".type","status":".status"}
4850
Conditions clusterv1.Conditions `json:"conditions,omitempty"`
4951
}
5052

@@ -85,6 +87,15 @@ func (c *VSphereClusterIdentity) SetConditions(conditions clusterv1.Conditions)
8587
// +kubebuilder:subresource:status
8688

8789
// VSphereClusterIdentity defines the account to be used for reconciling clusters
90+
// +Metrics:gvk:namePrefix="capi_vsphereclusteridentity"
91+
// +Metrics:labelFromPath:name="name",JSONPath=".metadata.name"
92+
// +Metrics:labelFromPath:name="namespace",JSONPath=".metadata.namespace"
93+
// +Metrics:labelFromPath:name="uid",JSONPath=".metadata.uid"
94+
// +Metrics:gauge:name="created",JSONPath=".metadata.creationTimestamp",help="Unix creation timestamp."
95+
// +Metrics:info:name="annotation_paused",JSONPath=.metadata.annotations['cluster\.x-k8s\.io/paused'],help="Whether the vsphereclusteridentity is paused and any of its resources will not be processed by the controllers.",labelsFromPath={paused_value:"."}
96+
// +Metrics:info:name="owner",JSONPath=".metadata.ownerReferences",help="Owner references.",labelsFromPath={owner_is_controller:".controller",owner_kind:".kind",owner_name:".name",owner_uid:".uid"}
97+
// +Metrics:labelFromPath:name="cluster_name",JSONPath=.metadata.labels.cluster\.x-k8s\.io/cluster-name
98+
// +Metrics:info:name="info",help="Information about a vsphereclusteridentity.",labelsFromPath={name:.metadata.name}
8899
type VSphereClusterIdentity struct {
89100
metav1.TypeMeta `json:",inline"`
90101
metav1.ObjectMeta `json:"metadata,omitempty"`

apis/v1beta1/vspheredeploymentzone_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ type VSphereDeploymentZoneStatus struct {
8181

8282
// Conditions defines current service state of the VSphereMachine.
8383
// +optional
84+
// +Metrics:stateset:name="status_condition",help="The condition of a vspheredeploymentzone.",labelName="status",JSONPath=".status",list={"True","False","Unknown"},labelsFromPath={"type":".type"}
85+
// +Metrics:gauge:name="status_condition_last_transition_time",help="The condition last transition time of a vspheredeploymentzone.",valueFrom=.lastTransitionTime,labelsFromPath={"type":".type","status":".status"}
8486
Conditions clusterv1.Conditions `json:"conditions,omitempty"`
8587
}
8688

@@ -90,6 +92,15 @@ type VSphereDeploymentZoneStatus struct {
9092
// +kubebuilder:subresource:status
9193

9294
// VSphereDeploymentZone is the Schema for the vspheredeploymentzones API
95+
// +Metrics:gvk:namePrefix="capi_vspheredeploymentzone"
96+
// +Metrics:labelFromPath:name="name",JSONPath=".metadata.name"
97+
// +Metrics:labelFromPath:name="namespace",JSONPath=".metadata.namespace"
98+
// +Metrics:labelFromPath:name="uid",JSONPath=".metadata.uid"
99+
// +Metrics:gauge:name="created",JSONPath=".metadata.creationTimestamp",help="Unix creation timestamp."
100+
// +Metrics:info:name="annotation_paused",JSONPath=.metadata.annotations['cluster\.x-k8s\.io/paused'],help="Whether the vspheredeploymentzone is paused and any of its resources will not be processed by the controllers.",labelsFromPath={paused_value:"."}
101+
// +Metrics:info:name="owner",JSONPath=".metadata.ownerReferences",help="Owner references.",labelsFromPath={owner_is_controller:".controller",owner_kind:".kind",owner_name:".name",owner_uid:".uid"}
102+
// +Metrics:labelFromPath:name="cluster_name",JSONPath=.metadata.labels.cluster\.x-k8s\.io/cluster-name
103+
// +Metrics:info:name="info",help="Information about a vspheredeploymentzone.",labelsFromPath={name:.metadata.name}
93104
type VSphereDeploymentZone struct {
94105
metav1.TypeMeta `json:",inline"`
95106
metav1.ObjectMeta `json:"metadata,omitempty"`

apis/v1beta1/vspherefailuredomain_types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ type FailureDomainHosts struct {
9595
// +kubebuilder:resource:path=vspherefailuredomains,scope=Cluster,categories=cluster-api
9696

9797
// VSphereFailureDomain is the Schema for the vspherefailuredomains API
98+
// +Metrics:gvk:namePrefix="capi_vspherefailuredomain"
99+
// +Metrics:labelFromPath:name="name",JSONPath=".metadata.name"
100+
// +Metrics:labelFromPath:name="namespace",JSONPath=".metadata.namespace"
101+
// +Metrics:labelFromPath:name="uid",JSONPath=".metadata.uid"
102+
// +Metrics:gauge:name="created",JSONPath=".metadata.creationTimestamp",help="Unix creation timestamp."
103+
// +Metrics:info:name="annotation_paused",JSONPath=.metadata.annotations['cluster\.x-k8s\.io/paused'],help="Whether the vspherefailuredomain is paused and any of its resources will not be processed by the controllers.",labelsFromPath={paused_value:"."}
104+
// +Metrics:info:name="owner",JSONPath=".metadata.ownerReferences",help="Owner references.",labelsFromPath={owner_is_controller:".controller",owner_kind:".kind",owner_name:".name",owner_uid:".uid"}
105+
// +Metrics:labelFromPath:name="cluster_name",JSONPath=.metadata.labels.cluster\.x-k8s\.io/cluster-name
106+
// +Metrics:info:name="info",help="Information about a vspherefailuredomain.",labelsFromPath={name:.metadata.name}
98107
type VSphereFailureDomain struct {
99108
metav1.TypeMeta `json:",inline"`
100109
metav1.ObjectMeta `json:"metadata,omitempty"`

apis/v1beta1/vspheremachine_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type VSphereMachineStatus struct {
7878
Ready bool `json:"ready"`
7979

8080
// Addresses contains the VSphere instance associated addresses.
81+
// +Metrics:info:name="status_addresses",help="Address information about a vspheremachine.",labelsFromPath={address:".address",type:".type"}
8182
Addresses []clusterv1.MachineAddress `json:"addresses,omitempty"`
8283

8384
// Network returns the network status for each of the machine's configured
@@ -125,6 +126,8 @@ type VSphereMachineStatus struct {
125126

126127
// Conditions defines current service state of the VSphereMachine.
127128
// +optional
129+
// +Metrics:stateset:name="status_condition",help="The condition of a vspheremachine.",labelName="status",JSONPath=".status",list={"True","False","Unknown"},labelsFromPath={"type":".type"}
130+
// +Metrics:gauge:name="status_condition_last_transition_time",help="The condition last transition time of a vspheremachine.",valueFrom=.lastTransitionTime,labelsFromPath={"type":".type","status":".status"}
128131
Conditions clusterv1.Conditions `json:"conditions,omitempty"`
129132
}
130133

@@ -139,6 +142,15 @@ type VSphereMachineStatus struct {
139142
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp",description="Time duration since creation of Machine"
140143

141144
// VSphereMachine is the Schema for the vspheremachines API
145+
// +Metrics:gvk:namePrefix="capi_vspheremachine"
146+
// +Metrics:labelFromPath:name="name",JSONPath=".metadata.name"
147+
// +Metrics:labelFromPath:name="namespace",JSONPath=".metadata.namespace"
148+
// +Metrics:labelFromPath:name="uid",JSONPath=".metadata.uid"
149+
// +Metrics:gauge:name="created",JSONPath=".metadata.creationTimestamp",help="Unix creation timestamp."
150+
// +Metrics:info:name="annotation_paused",JSONPath=.metadata.annotations['cluster\.x-k8s\.io/paused'],help="Whether the vspheremachine is paused and any of its resources will not be processed by the controllers.",labelsFromPath={paused_value:"."}
151+
// +Metrics:info:name="owner",JSONPath=".metadata.ownerReferences",help="Owner references.",labelsFromPath={owner_is_controller:".controller",owner_kind:".kind",owner_name:".name",owner_uid:".uid"}
152+
// +Metrics:labelFromPath:name="cluster_name",JSONPath=.metadata.labels.cluster\.x-k8s\.io/cluster-name
153+
// +Metrics:info:name="info",help="Information about a vspheremachine.",labelsFromPath={provider_id:.spec.providerID}
142154
type VSphereMachine struct {
143155
metav1.TypeMeta `json:",inline"`
144156
metav1.ObjectMeta `json:"metadata,omitempty"`

apis/v1beta1/vspherevm_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type VSphereVMStatus struct {
104104
// This field is required at runtime for other controllers that read
105105
// this CRD as unstructured data.
106106
// +optional
107+
// +Metrics:info:name="status_addresses",help="Address information about a vspherevm.",labelsFromPath={address:"."}
107108
Addresses []string `json:"addresses,omitempty"`
108109

109110
// CloneMode is the type of clone operation used to clone this VM. Since
@@ -165,6 +166,8 @@ type VSphereVMStatus struct {
165166

166167
// Conditions defines current service state of the VSphereVM.
167168
// +optional
169+
// +Metrics:stateset:name="status_condition",help="The condition of a vspherevm.",labelName="status",JSONPath=".status",list={"True","False","Unknown"},labelsFromPath={"type":".type"}
170+
// +Metrics:gauge:name="status_condition_last_transition_time",help="The condition last transition time of a vspherevm.",valueFrom=.lastTransitionTime,labelsFromPath={"type":".type","status":".status"}
168171
Conditions clusterv1.Conditions `json:"conditions,omitempty"`
169172

170173
// ModuleUUID is the unique identifier for the vCenter cluster module construct
@@ -187,6 +190,15 @@ type VSphereVMStatus struct {
187190
// +kubebuilder:subresource:status
188191

189192
// VSphereVM is the Schema for the vspherevms API
193+
// +Metrics:gvk:namePrefix="capi_vspherevm"
194+
// +Metrics:labelFromPath:name="name",JSONPath=".metadata.name"
195+
// +Metrics:labelFromPath:name="namespace",JSONPath=".metadata.namespace"
196+
// +Metrics:labelFromPath:name="uid",JSONPath=".metadata.uid"
197+
// +Metrics:gauge:name="created",JSONPath=".metadata.creationTimestamp",help="Unix creation timestamp."
198+
// +Metrics:info:name="annotation_paused",JSONPath=.metadata.annotations['cluster\.x-k8s\.io/paused'],help="Whether the vspherevm is paused and any of its resources will not be processed by the controllers.",labelsFromPath={paused_value:"."}
199+
// +Metrics:info:name="owner",JSONPath=".metadata.ownerReferences",help="Owner references.",labelsFromPath={owner_is_controller:".controller",owner_kind:".kind",owner_name:".name",owner_uid:".uid"}
200+
// +Metrics:labelFromPath:name="cluster_name",JSONPath=.metadata.labels.cluster\.x-k8s\.io/cluster-name
201+
// +Metrics:info:name="info",help="Information about a vspherevm.",labelsFromPath={status_clonemode:.status.cloneMode,bootstrap_reference_kind:".spec.bootstrapRef.kind",bootstrap_reference_name:".spec.bootstrapRef.name",status_vmref:".status.vmRef"}
190202
type VSphereVM struct {
191203
metav1.TypeMeta `json:",inline"`
192204
metav1.ObjectMeta `json:"metadata,omitempty"`

hack/go-tools-build.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ if [ -z "${GOBIN}" ]; then
3737
exit 1
3838
fi
3939

40-
CAPI_HACK_TOOLS="sigs.k8s.io/cluster-api/hack/tools"
40+
export GOWORK="off"
41+
42+
GOMOD_REPLACE=${GOMOD_REPLACE:=}
43+
GOMOD_REQUIRE=${GOMOD_REQUIRE:=}
4144

4245
rm -f "${GOBIN}/${2}"* || true
4346

@@ -52,8 +55,13 @@ cd "${TMP_MODULE_DIR}"
5255

5356
# Initialize a go module and place a tools.go file for building the binary.
5457
go mod init "tools"
55-
# Set require for "sigs.k8s.io/cluster-api" to let go resolve the tools version via the CAPI version.
56-
go mod edit -require "${CAPI_HACK_TOOLS}@${3}"
58+
59+
for PARAM in ${GOMOD_REPLACE}; do
60+
eval go mod edit -replace "${PARAM}"
61+
done
62+
for PARAM in ${GOMOD_REQUIRE}; do
63+
eval go mod edit -require "${PARAM}"
64+
done
5765

5866
# Create go file which imports the required package and resolve dependencies.
5967
cat << EOF > tools.go

0 commit comments

Comments
 (0)