Skip to content

Commit

Permalink
Set workload target reference in analytics status
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyufei authored and yufeiyu committed Mar 8, 2022
1 parent f0b540f commit a909e4e
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 43 deletions.
87 changes: 62 additions & 25 deletions deploy/manifests/analysis.crane.io_analytics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -213,31 +213,6 @@ spec:
description: Recommendations is a list of pointers to recommendations
that are updated by this analytics.
items:
description: 'ObjectReference contains enough information to let
you inspect or modify the referred object. --- New uses of this
type are discouraged because of difficulty describing its usage
when embedded in APIs. 1. Ignored fields. It includes many fields
which are not generally honored. For instance, ResourceVersion
and FieldPath are both very rarely valid in actual usage. 2.
Invalid usage help. It is impossible to add specific help for
individual usage. In most embedded usages, there are particular restrictions
like, "must refer only to types A and B" or "UID not honored"
or "name must be restricted". Those cannot be well described
when embedded. 3. Inconsistent validation. Because the usages
are different, the validation rules are different by usage, which
makes it hard for users to predict what will happen. 4. The fields
are both imprecise and overly precise. Kind is not a precise
mapping to a URL. This can produce ambiguity during interpretation
and require a REST mapping. In most cases, the dependency is
on the group,resource tuple and the version of the actual
struct is irrelevant. 5. We cannot easily change it. Because
this type is embedded in many locations, updates to this type will
affect numerous schemas. Don''t make new APIs embed an underspecified
API type they do not control. Instead of using this type, create
a locally provided and used type that is well-focused on your
reference. For example, ServiceReferences for admission registration:
https://github.com/kubernetes/api/blob/release-1.17/admissionregistration/v1/types.go#L533
.'
properties:
apiVersion:
description: API version of the referent.
Expand Down Expand Up @@ -268,6 +243,68 @@ spec:
description: 'Specific resourceVersion to which this reference
is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency'
type: string
targetRef:
description: 'ObjectReference contains enough information to
let you inspect or modify the referred object. --- New uses
of this type are discouraged because of difficulty describing
its usage when embedded in APIs. 1. Ignored fields. It includes
many fields which are not generally honored. For instance,
ResourceVersion and FieldPath are both very rarely valid in
actual usage. 2. Invalid usage help. It is impossible to
add specific help for individual usage. In most embedded
usages, there are particular restrictions like, "must
refer only to types A and B" or "UID not honored" or "name
must be restricted". Those cannot be well described when
embedded. 3. Inconsistent validation. Because the usages
are different, the validation rules are different by usage,
which makes it hard for users to predict what will happen. 4.
The fields are both imprecise and overly precise. Kind is
not a precise mapping to a URL. This can produce ambiguity during
interpretation and require a REST mapping. In most cases,
the dependency is on the group,resource tuple and the
version of the actual struct is irrelevant. 5. We cannot
easily change it. Because this type is embedded in many locations,
updates to this type will affect numerous schemas. Don''t
make new APIs embed an underspecified API type they do not
control. Instead of using this type, create a locally provided
and used type that is well-focused on your reference. For
example, ServiceReferences for admission registration: https://github.com/kubernetes/api/blob/release-1.17/admissionregistration/v1/types.go#L533
.'
properties:
apiVersion:
description: API version of the referent.
type: string
fieldPath:
description: 'If referring to a piece of an object instead
of an entire object, this string should contain a valid
JSON/Go field access statement, such as desiredState.manifest.containers[2].
For example, if the object reference is to a container
within a pod, this would take on a value like: "spec.containers{name}"
(where "name" refers to the name of the container that
triggered the event) or if no container name is specified
"spec.containers[2]" (container with index 2 in this pod).
This syntax is chosen only to have some well-defined way
of referencing a part of an object. TODO: this design
is not final and this field is subject to change in the
future.'
type: string
kind:
description: 'Kind of the referent. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
type: string
namespace:
description: 'Namespace of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/'
type: string
resourceVersion:
description: 'Specific resourceVersion to which this reference
is made, if any. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency'
type: string
uid:
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
type: string
type: object
uid:
description: 'UID of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#uids'
type: string
Expand Down
9 changes: 9 additions & 0 deletions deploy/manifests/analysis.crane.io_recommendations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ spec:
description: RecommendationSpec describes the recommendation type and
what the recommendation is for.
properties:
adoptionType:
default: StatusAndAnnotation
description: AdoptionType indicate how to adopt recommendation value
to target. the default AdoptionType is StatusAndAnnotation.
enum:
- Status
- StatusAndAnnotation
- Auto
type: string
completionStrategy:
description: CompletionStrategy indicate how to complete a recommendation.
the default CompletionStrategy is Once.
Expand Down
111 changes: 110 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/go-echarts/go-echarts/v2 v2.2.4
github.com/gocrane/api v0.0.0-20220303034349-4196a7186df2
github.com/gocrane/api v0.2.1-0.20220308075341-c4f28c1981e6
github.com/google/cadvisor v0.39.2
github.com/mjibson/go-dsp v0.0.0-20180508042940-11479a337f12
github.com/prometheus/client_golang v1.11.0
Expand All @@ -30,6 +30,115 @@ require (
sigs.k8s.io/custom-metrics-apiserver v1.22.0
)

require (
github.com/NYTimes/gziphandler v1.1.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.2.0 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/checkpoint-restore/go-criu/v5 v5.0.0 // indirect
github.com/cilium/ebpf v0.6.2 // indirect
github.com/containerd/console v1.0.2 // indirect
github.com/containerd/containerd v1.4.4 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/cyphar/filepath-securejoin v0.2.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v20.10.2+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/emicklei/go-restful v2.15.0+incompatible // indirect
github.com/emicklei/go-restful-swagger12 v0.0.0-20201014110547-68ccff494617 // indirect
github.com/euank/go-kmsg-parser v2.0.0+incompatible // indirect
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v0.4.0 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/godbus/dbus/v5 v5.0.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/gosimple/slug v1.1.1 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/karrick/godirwalk v1.16.1 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mindprince/gonvml v0.0.0-20190828220739-9ebdce4bb989 // indirect
github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible // indirect
github.com/moby/sys/mountinfo v0.4.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mrunalp/fileutils v0.5.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v1.0.2 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
github.com/opencontainers/selinux v1.8.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be // indirect
github.com/seccomp/libseccomp-golang v0.9.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
github.com/tklauser/numcpus v0.3.0 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/vishvananda/netlink v1.1.0 // indirect
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae // indirect
go.etcd.io/etcd/api/v3 v3.5.1 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.1 // indirect
go.etcd.io/etcd/client/v3 v3.5.0 // indirect
go.opentelemetry.io/contrib v0.20.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.20.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.20.0 // indirect
go.opentelemetry.io/otel v0.20.0 // indirect
go.opentelemetry.io/otel/exporters/otlp v0.20.0 // indirect
go.opentelemetry.io/otel/metric v0.20.0 // indirect
go.opentelemetry.io/otel/sdk v0.20.0 // indirect
go.opentelemetry.io/otel/sdk/export/metric v0.20.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.20.0 // indirect
go.opentelemetry.io/otel/trace v0.20.0 // indirect
go.opentelemetry.io/proto/otlp v0.7.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.19.0 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/warnings.v0 v0.1.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/apiextensions-apiserver v0.22.2 // indirect
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.0.22 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)

require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/gin-contrib/cors v1.3.1
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,10 @@ github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.1.0-rc.5 h1:QOAag7FoBaBYYHRqzqkhhd8fq5RTubvI4v3Ft/gDVVQ=
github.com/gobwas/ws v1.1.0-rc.5/go.mod h1:nzvNcVha5eUziGrbxFCo6qFIojQHjJV5cLYIbezhfL0=
github.com/gocrane/api v0.0.0-20220303034349-4196a7186df2 h1:EOKCLJVPmzEAbaKmkUvX7RLRsc+bXrriCuixiUH4WGA=
github.com/gocrane/api v0.0.0-20220303034349-4196a7186df2/go.mod h1:GxI+t9AW8+NsHkz2JkPBIJN//9eLUjTZl1ScYAbXMbk=
github.com/gocrane/api v0.2.0 h1:6e8urUDsN8dx/KvLk01m8uMsox+2Y09N9rm6H9AquIM=
github.com/gocrane/api v0.2.0/go.mod h1:GxI+t9AW8+NsHkz2JkPBIJN//9eLUjTZl1ScYAbXMbk=
github.com/gocrane/api v0.2.1-0.20220308075341-c4f28c1981e6 h1:++2IaGyBIoUff3082SG7BNjBR4Nn/X4qjH6+bnzJIv4=
github.com/gocrane/api v0.2.1-0.20220308075341-c4f28c1981e6/go.mod h1:GxI+t9AW8+NsHkz2JkPBIJN//9eLUjTZl1ScYAbXMbk=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand Down
37 changes: 22 additions & 15 deletions pkg/controller/analytics/analytics_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,19 @@ func (c *Controller) DoAnalytics(ctx context.Context, analytics *analysisv1alph1
}
}

var refs []corev1.ObjectReference
var refs []analysisv1alph1.RecommendationReference

for k, id := range identities {
if r, exists := recommendationMap[k]; exists {
refs = append(refs, corev1.ObjectReference{
Kind: recommendationMap[k].Kind,
Name: recommendationMap[k].Name,
Namespace: recommendationMap[k].Namespace,
APIVersion: recommendationMap[k].APIVersion,
UID: recommendationMap[k].UID,
refs = append(refs, analysisv1alph1.RecommendationReference{
ObjectReference: corev1.ObjectReference{
Kind: recommendationMap[k].Kind,
Name: recommendationMap[k].Name,
Namespace: recommendationMap[k].Namespace,
APIVersion: recommendationMap[k].APIVersion,
UID: recommendationMap[k].UID,
},
TargetRef: recommendationMap[k].Spec.TargetRef,
})
found := false
for _, or := range r.OwnerReferences {
Expand Down Expand Up @@ -202,8 +205,9 @@ func (c *Controller) DoAnalytics(ctx context.Context, analytics *analysisv1alph1
}

func (c *Controller) CreateRecommendation(ctx context.Context, analytics *analysisv1alph1.Analytics,
id ObjectIdentity, refs *[]corev1.ObjectReference) error {
id ObjectIdentity, refs *[]analysisv1alph1.RecommendationReference) error {

targetRef := corev1.ObjectReference{Kind: id.Kind, APIVersion: id.APIVersion, Namespace: id.Namespace, Name: id.Name}
recommendation := &analysisv1alph1.Recommendation{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-%s-", analytics.Name, strings.ToLower(string(analytics.Spec.Type))),
Expand All @@ -214,7 +218,7 @@ func (c *Controller) CreateRecommendation(ctx context.Context, analytics *analys
Labels: id.Labels,
},
Spec: analysisv1alph1.RecommendationSpec{
TargetRef: corev1.ObjectReference{Kind: id.Kind, APIVersion: id.APIVersion, Namespace: id.Namespace, Name: id.Name},
TargetRef: targetRef,
Type: analytics.Spec.Type,
CompletionStrategy: analytics.Spec.CompletionStrategy,
},
Expand All @@ -227,12 +231,15 @@ func (c *Controller) CreateRecommendation(ctx context.Context, analytics *analys

klog.InfoS("Successful to create", "Recommendation", klog.KObj(recommendation), "Analytics", klog.KObj(analytics))

*refs = append(*refs, corev1.ObjectReference{
Kind: recommendation.Kind,
Name: recommendation.Name,
Namespace: recommendation.Namespace,
APIVersion: recommendation.APIVersion,
UID: recommendation.UID,
*refs = append(*refs, analysisv1alph1.RecommendationReference{
ObjectReference: corev1.ObjectReference{
Kind: recommendation.Kind,
Name: recommendation.Name,
Namespace: recommendation.Namespace,
APIVersion: recommendation.APIVersion,
UID: recommendation.UID,
},
TargetRef: targetRef,
})

return nil
Expand Down

0 comments on commit a909e4e

Please sign in to comment.