Skip to content
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

feat: handles cleanup by using owner ref and cleanup hook #4

Merged
merged 8 commits into from
Jul 14, 2023
46 changes: 42 additions & 4 deletions apis/ossm.plugins.kubeflow.org/v1alpha1/ossm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ type OssmPlugin struct {
Status OssmPluginStatus `json:"status,omitempty"`
}

// OssmPluginSpec defines the extra data provided by the Openshift Service Mesh Plugin in KfDef spec.
// OssmPluginSpec defines configuration needed for Openshift Service Mesh
// for integration with Opendatahub.
type OssmPluginSpec struct {
Mesh MeshSpec `json:"mesh,omitempty"`
Auth AuthSpec `json:"auth,omitempty"`
}

// MeshSpec holds information on how Service Mesh should be configured.
type MeshSpec struct {
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Certificate CertSpec `json:"certificate,omitempty"`
}

type CertSpec struct {
Name string `json:"name,omitempty" default:"opendatahub-self-signed-cert"`
Name string `json:"name,omitempty"`
cam-garrison marked this conversation as resolved.
Show resolved Hide resolved
Generate bool `json:"generate,omitempty"`
}

Expand All @@ -55,13 +57,49 @@ type OssmPluginStatus struct {

//+kubebuilder:object:root=true

// OssmPluginList contains a list of GcpPlugin
// OssmPluginList contains a list of OssmPlugins
type OssmPluginList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []OssmPlugin `json:"items"`
}

// OssmResourceTracker is a cluster-scoped resource for tracking objects
// created by Ossm plugin. It's primarily used as owner reference
// for resources created across namespaces so that they can be
// garbage collected by Kubernetes when they're not needed anymore.
// +kubebuilder:object:root=true
// +kubebuilder:resource:scope=Cluster
type OssmResourceTracker struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec OssmResourceTrackerSpec `json:"spec,omitempty"`
Status OssmResourceTrackerStatus `json:"status,omitempty"`
}

// OssmResourceTrackerSpec defines the desired state of OssmResourceTracker
type OssmResourceTrackerSpec struct {
}

// OssmResourceTrackerStatus defines the observed state of OssmResourceTracker
type OssmResourceTrackerStatus struct {
}

// +kubebuilder:object:root=true

// OssmResourceTrackerList contains a list of OssmResourceTracker
type OssmResourceTrackerList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []OssmResourceTracker `json:"items"`
}

func init() {
SchemeBuilder.Register(&OssmPlugin{}, &OssmPluginList{})
SchemeBuilder.Register(
&OssmPlugin{},
&OssmPluginList{},
&OssmResourceTracker{},
&OssmResourceTrackerList{},
)
}
89 changes: 89 additions & 0 deletions apis/ossm.plugins.kubeflow.org/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.2
creationTimestamp: null
name: ossmresourcetrackers.ossm.plugins.kubeflow.org
spec:
group: ossm.plugins.kubeflow.org
names:
kind: OssmResourceTracker
listKind: OssmResourceTrackerList
plural: ossmresourcetrackers
singular: ossmresourcetracker
scope: Cluster
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: OssmResourceTracker is a cluster-scoped resource for tracking
objects created by Ossm plugin. It's primarily used as owner reference for
resources created across namespaces so that they can be garbage collected
by Kubernetes when they're not needed anymore.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: OssmResourceTrackerSpec defines the desired state of OssmResourceTracker
type: object
status:
description: OssmResourceTrackerStatus defines the observed state of OssmResourceTracker
type: object
type: object
served: true
storage: true
status:
acceptedNames:
kind: ""
plural: ""
conditions: null
storedVersions: null
6 changes: 4 additions & 2 deletions config/crd/bases/ossm.plugins.kubeflow.org_ossmplugins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ spec:
metadata:
type: object
spec:
description: OssmPluginSpec defines the extra data provided by the Openshift
Service Mesh Plugin in KfDef spec.
description: OssmPluginSpec defines configuration needed for Openshift
Service Mesh for integration with Opendatahub.
properties:
auth:
properties:
Expand All @@ -52,6 +52,8 @@ spec:
type: string
type: object
mesh:
description: MeshSpec holds information on how Service Mesh should
be configured.
properties:
certificate:
properties:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.9.2
creationTimestamp: null
name: ossmresourcetrackers.ossm.plugins.kubeflow.org
spec:
group: ossm.plugins.kubeflow.org
names:
kind: OssmResourceTracker
listKind: OssmResourceTrackerList
plural: ossmresourcetrackers
singular: ossmresourcetracker
scope: Cluster
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: OssmResourceTracker is a cluster-scoped resource for tracking
objects created by Ossm plugin. It's primarily used as owner reference for
resources created across namespaces so that they can be garbage collected
by Kubernetes when they're not needed anymore.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: OssmResourceTrackerSpec defines the desired state of OssmResourceTracker
type: object
status:
description: OssmResourceTrackerStatus defines the observed state of OssmResourceTracker
type: object
type: object
served: true
storage: true
1 change: 1 addition & 0 deletions config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
resources:
- bases/kfdef.apps.kubeflow.org_kfdefs.yaml
- bases/ossm.plugins.kubeflow.org_ossmplugins.yaml
- bases/ossm.plugins.kubeflow.org_ossmresourcetrackers.yaml
- dashboard-crds/odhapplications.dashboard.opendatahub.io.crd.yaml
- dashboard-crds/odhdashboardconfigs.opendatahub.io.crd.yaml
- dashboard-crds/odhdocuments.dashboard.opendatahub.io.crd.yaml
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func main() {
os.Exit(1)
}

setupLog.Info("starting manager 123 !!!!")
setupLog.Info("starting manager")
cam-garrison marked this conversation as resolved.
Show resolved Hide resolved
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
Expand Down
17 changes: 17 additions & 0 deletions pkg/kfapp/coordinator/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,22 @@ func (kfapp *coordinator) Delete(resources kftypesv3.ResourceEnum) error {
return nil
}

ossmCleanup := func() error {
if kfapp.KfDef.Spec.Platform != kftypesv3.OSSM {
return nil
}

if p, ok := kfapp.Platforms[kfapp.KfDef.Spec.Platform]; !ok {
return &kfapis.KfError{
Code: int(kfapis.INTERNAL_ERROR),
Message: "Platform OSSM specified but not loaded.",
}
} else {
ossmInstaller := p.(*ossm.OssmInstaller)
return ossmInstaller.CleanupOwnedResources()
}
}

if err := kfapp.KfDef.SyncCache(); err != nil {
return &kfapis.KfError{
Code: int(kfapis.INTERNAL_ERROR),
Expand Down Expand Up @@ -499,6 +515,7 @@ func (kfapp *coordinator) Delete(resources kftypesv3.ResourceEnum) error {
if err := k8s(); err != nil {
return err
}
return ossmCleanup()
}
return nil
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/kfapp/ossm/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"math/big"
"math/rand"
"net"
Expand All @@ -22,13 +21,22 @@ import (

var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))

func createSelfSignedCerts(config *rest.Config, addr string, objectMeta metav1.ObjectMeta) error {
func (o *OssmInstaller) createSelfSignedCerts(addr string, objectMeta metav1.ObjectMeta) error {

cert, key, err := generateCertificate(addr)
if err != nil {
return errors.WithStack(err)
}

objectMeta.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: o.tracker.APIVersion,
Kind: o.tracker.Kind,
Name: o.tracker.Name,
UID: o.tracker.UID,
},
})
Comment on lines +31 to +38
Copy link
Author

Choose a reason for hiding this comment

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

Note to self: we have it in multiple places - worth extracting


secret := &corev1.Secret{
ObjectMeta: objectMeta,
Data: map[string][]byte{
Expand All @@ -37,7 +45,7 @@ func createSelfSignedCerts(config *rest.Config, addr string, objectMeta metav1.O
},
}

clientset, err := kubernetes.NewForConfig(config)
clientset, err := kubernetes.NewForConfig(o.config)
if err != nil {
return errors.WithStack(err)
}
Expand Down
Loading