Skip to content

Commit

Permalink
feat: add standard platform labels to the created resources
Browse files Browse the repository at this point in the history
+ lint minor linter changes
  • Loading branch information
aslakknutsen committed Jun 15, 2024
1 parent abfe539 commit 4a305b7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 33 deletions.
5 changes: 2 additions & 3 deletions controllers/authorization/authorization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ func (r *PlatformAuthorizationReconciler) Reconcile(ctx context.Context, req ctr

if err := r.Client.Get(ctx, req.NamespacedName, sourceRes); err != nil {
if apierrs.IsNotFound(err) {
r.log.Info("Stopping reconciliation")
r.log.Info("stopping reconciliation")

return ctrl.Result{}, nil
}

return ctrl.Result{}, errors.Wrap(err, "failed getting service")
}

r.log.Info("Triggered Auth Reconcile", "namespace", req.Namespace, "name", req.Name)
r.log.Info("triggered auth reconcile", "namespace", req.Namespace, "name", req.Name)

var errs []error
for _, reconciler := range reconcilers {
Expand All @@ -83,7 +83,6 @@ func (r *PlatformAuthorizationReconciler) SetupWithManager(mgr ctrl.Manager) err
Kind: r.authComponent.CustomResourceType.Kind,
},
}, builder.OnlyMetadata).
// TODO: Add OwnerRef predicator on GVK?
Owns(&authorinov1beta2.AuthConfig{}).
Owns(&istiosecv1beta1.AuthorizationPolicy{}).
Owns(&istiosecv1beta1.PeerAuthentication{}).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

authorinov1beta2 "github.com/kuadrant/authorino/api/v1beta2"
"github.com/opendatahub-io/odh-platform/pkg/env"
"github.com/opendatahub-io/odh-platform/pkg/label"
"github.com/pkg/errors"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -64,7 +65,6 @@ func (r *PlatformAuthorizationReconciler) reconcileAuthConfig(ctx context.Contex

found.Spec = *desired.Spec.DeepCopy()
found.ObjectMeta.Labels = desired.ObjectMeta.Labels
// TODO: Merge Annotations?

return errors.Wrap(r.Update(ctx, found), "failed updating AuthConfig")
}); err != nil {
Expand All @@ -89,8 +89,8 @@ func createAuthConfig(templ authorinov1beta2.AuthConfig, hosts []string, target
labels[authKey] = authVal
templ.Name = target.GetName()
templ.Namespace = target.GetNamespace()
templ.Labels = labels // TODO: Where to fetch lables from
templ.Annotations = map[string]string{} // TODO: where to fetch annotations from? part-of "service comp" or "platform?"
templ.Labels = label.ApplyStandard(target.GetLabels())
templ.Annotations = map[string]string{}
templ.Spec.Hosts = hosts
templ.OwnerReferences = []metav1.OwnerReference{
targetToOwnerRef(target),
Expand All @@ -99,7 +99,6 @@ func createAuthConfig(templ authorinov1beta2.AuthConfig, hosts []string, target
return &templ, nil
}

// TODO: We have multiple Controllers adding Spec.Hosts. Compare specifically that the ones we need are in the list, if more assume equal?
func CompareAuthConfigs(m1, m2 *authorinov1beta2.AuthConfig) bool {
return reflect.DeepEqual(m1.ObjectMeta.Labels, m2.ObjectMeta.Labels) &&
reflect.DeepEqual(m1.Spec, m2.Spec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"

"github.com/opendatahub-io/odh-platform/pkg/env"
"github.com/opendatahub-io/odh-platform/pkg/label"
"github.com/pkg/errors"
"istio.io/api/security/v1beta1"
istiotypev1beta1 "istio.io/api/type/v1beta1"
Expand Down Expand Up @@ -50,7 +51,6 @@ func (r *PlatformAuthorizationReconciler) reconcileAuthPolicy(ctx context.Contex

found.Spec = *desired.Spec.DeepCopy()
found.ObjectMeta.Labels = desired.ObjectMeta.Labels
// TODO: Merge Annotations?

return errors.Wrap(r.Update(ctx, found), "failed updating AuthorizationPolicy")
}); err != nil {
Expand All @@ -61,14 +61,13 @@ func (r *PlatformAuthorizationReconciler) reconcileAuthPolicy(ctx context.Contex
return nil
}

// TODO: Owned by?
func createAuthorizationPolicy(ports []string, workloadSelector map[string]string, target *unstructured.Unstructured) *istiosecv1beta1.AuthorizationPolicy {
policy := &istiosecv1beta1.AuthorizationPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: target.GetName(),
Namespace: target.GetNamespace(),
Labels: target.GetLabels(), // TODO: Where to fetch lables from
Annotations: map[string]string{}, // TODO: where to fetch annotations from? part-of "service comp" or "platform?"
Labels: label.ApplyStandard(target.GetLabels()),
Annotations: map[string]string{},
OwnerReferences: []metav1.OwnerReference{
targetToOwnerRef(target),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"reflect"

"github.com/opendatahub-io/odh-platform/pkg/label"
"github.com/pkg/errors"
"istio.io/api/security/v1beta1"
istiotypev1beta1 "istio.io/api/type/v1beta1"
Expand Down Expand Up @@ -49,7 +50,6 @@ func (r *PlatformAuthorizationReconciler) reconcilePeerAuthentication(ctx contex

found.Spec = *desired.Spec.DeepCopy()
found.ObjectMeta.Labels = desired.ObjectMeta.Labels
// TODO: Merge Annotations?

return errors.Wrap(r.Update(ctx, found), "failed updating PeerAuthentication")
}); err != nil {
Expand All @@ -65,8 +65,8 @@ func createPeerAuthentication(workloadSelector map[string]string, target *unstru
ObjectMeta: metav1.ObjectMeta{
Name: target.GetName(),
Namespace: target.GetNamespace(),
Labels: target.GetLabels(), // TODO: Where to fetch lables from
Annotations: map[string]string{}, // TODO: where to fetch annotations from? part-of "service comp" or "platform?"
Labels: label.ApplyStandard(target.GetLabels()),
Annotations: map[string]string{},
OwnerReferences: []metav1.OwnerReference{
targetToOwnerRef(target),
},
Expand Down
17 changes: 1 addition & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,7 @@ func main() {
setupLog.Error(err, "unable to load config from "+env.GetConfigFile())
os.Exit(1)
}
/*
components := []spi.AuthorizationComponent{
{
CustomResourceType: schema.GroupVersionKind{
Group: "modelregistry.opendatahub.io",
Version: "v1alpha1",
Kind: "ModelRegistry",
},
WorkloadSelector: map[string]string{
"component": "model-registry",
},
Ports: []string{"8080", "9090"},
HostPaths: []string{"status.url"},
},
}
*/

for _, component := range components {
if err = authorization.NewPlatformAuthorizationReconciler(mgr.GetClient(), ctrlLog, component).
SetupWithManager(mgr); err != nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/label/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package label

import "github.com/opendatahub-io/odh-platform/version"

const (
LabelAppPartOf = "app.kubernetes.io/part-of"
LabelAppComponent = "app.kubernetes.io/component"
LabelAppName = "app.kubernetes.io/name"
LabelAppVersion = "app.kubernetes.io/version"
LabelAppManagedBy = "app.kubernetes.io/managed-by"
LabelODHComponent = "platform.opendatahub.io/component"
LabelODHDSC = "platform.opendatahub.io/data-science-cluster"
LabelODHDSP = "platform.opendatahub.io/data-science-project"
)

func ApplyStandard(source map[string]string) map[string]string {
target := map[string]string{}

target[LabelAppPartOf] = source[LabelAppName]
target[LabelAppComponent] = source[LabelAppComponent]

target[LabelAppVersion] = version.Version
target[LabelAppManagedBy] = "odh-platform"

return target
}
6 changes: 3 additions & 3 deletions pkg/resource/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ func LoadConfig(path string) ([]spi.AuthorizationComponent, error) {
return []spi.AuthorizationComponent{}, errors.Wrap(err, "could not read config file "+path)
}

var capabilities capabilities
var caps capabilities

err = json.Unmarshal(content, &capabilities)
err = json.Unmarshal(content, &caps)
if err != nil {
return []spi.AuthorizationComponent{}, errors.Wrap(err, "could not parse json content of "+path)
}

for _, v := range capabilities.Authorization {
for _, v := range caps.Authorization {
components = append(components, v...)
}

Expand Down

0 comments on commit 4a305b7

Please sign in to comment.