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

Add basic functionality #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions api/v1alpha1/clustermanagedresource_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2024.
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 v1alpha1

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// NamespaceSelector provides a way to specify targeted namespaces
type NamespaceSelector struct {
// LabelSelector of namespaces to be targeted.
// Can be combined with MatchNames to include unlabelled namespaces.
LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"`

// MatchNames lists namespace names to be targeted.
// Each entry can be a Regex pattern.
// A namespace is included if at least one pattern matches.
// Invalid patterns will cause the sync to be cancelled and the status conditions will contain the error message.
MatchNames []string `json:"matchNames,omitempty"`

// IgnoreNames lists namespace names to be ignored.
// Each entry can be a Regex pattern and if they match
// the namespaces will be excluded from the sync even if matching in "matchNames" or via LabelSelector.
// A namespace is ignored if at least one pattern matches.
// Invalid patterns will cause the sync to be cancelled and the status conditions will contain the error message.
IgnoreNames []string `json:"ignoreNames,omitempty"`
}

// 👇 Finish the description
// ClusterManagedResourceSpec defines the desired state of ClusterManagedResource.
type ClusterManagedResourceSpec struct {
// +kubebuilder:validation:Optional
Context []Context `json:"context,omitempty"`

// +kubebuilder:validation:Required
// Template defines the Jsonnet snippet that generates the output.
Template string `json:"template"`

// +kubebuilder:validation:Optional
NamespaceSelector NamespaceSelector `json:"namespaceSelector,omitempty"`
}

// ClusterManagedResourceStatus defines the observed state of ClusterManagedResource.
type ClusterManagedResourceStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// ClusterManagedResource is the Schema for the clustermanagedresources API.
type ClusterManagedResource struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ClusterManagedResourceSpec `json:"spec,omitempty"`
Status ClusterManagedResourceStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&ClusterManagedResource{}, &ClusterManagedResourceList{})
}

func (mr *ClusterManagedResource) GetParserName() string {
return fmt.Sprintf("cluster/%s", mr.Name)
}

func (mr *ClusterManagedResource) GetContext() []Context {
return mr.Spec.Context
}

func (mr *ClusterManagedResource) GetTemplate() string {
return mr.Spec.Template
}

func (mr *ClusterManagedResource) GetNamespaceSelector() NamespaceSelector {
return mr.Spec.NamespaceSelector
}

func (mr *ClusterManagedResource) IsNamespaced() bool {
return false
}
36 changes: 36 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2024.
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 v1alpha1 contains API Schema definitions for the espejo v1alpha1 API group.
// +kubebuilder:object:generate=true
// +groupName=espejo.appuio.io
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects.
GroupVersion = schema.GroupVersion{Group: "espejo.appuio.io", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
117 changes: 117 additions & 0 deletions api/v1alpha1/managedresource_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
Copyright 2024.
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 v1alpha1

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// 👇 Finish the description
// ManagedResourceSpec defines the <INSERT_TEXT>.
type ManagedResourceSpec struct {
// +kubebuilder:validation:Optional
Context []Context `json:"context,omitempty"`

// +kubebuilder:validation:Required
// Template defines the Jsonnet snippet that generates the output.
Template string `json:"template"`
}

// Context defines resources used in the template.
// Changes to these resources will trigger a reconcile the ManagedResource.
type Context struct {
// +kubebuilder:validation:Required
// Alias defines the name of the ext-var in the Jsonnet template.
Alias string `json:"alias"`

// +kubebuilder:validation:Required
// APIVersion defines the versioned schema of this representation of an object.
APIVersion string `json:"apiVersion"`

// +kubebuilder:validation:Required
// Kind is a string value representing the REST resource this object represents.
Kind string `json:"kind"`

// +kubebuilder:validation:Optional
// Name defines the name of the resource to be targeted.
// If left empty all resources of that kind will be targeted.
Name string `json:"name,omitempty"`

// +kubebuilder:validation:Optional
// Namespace defines the namespaces of the resource to be targeted.
// If left empty all valid namespaces will be targeted.
Namespace string `json:"namespace,omitempty"`

// +kubebuilder:validation:Optional
// LabelSelector filters results by label.
LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"`
}

// ManagedResourceStatus defines the observed state of ManagedResource.
type ManagedResourceStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// ManagedResource is the Schema for the managedresources API.
type ManagedResource struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ManagedResourceSpec `json:"spec,omitempty"`
Status ManagedResourceStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&ManagedResource{}, &ManagedResourceList{})
}

func (mr *ManagedResource) GetParserName() string {
return fmt.Sprintf("%s/%s", mr.Namespace, mr.Name)
}

func (mr *ManagedResource) GetContext() []Context {
return mr.Spec.Context
}

func (mr *ManagedResource) GetTemplate() string {
return mr.Spec.Template
}

func (mr *ManagedResource) GetNamespaceSelector() NamespaceSelector {
return NamespaceSelector{
MatchNames: []string{mr.Namespace},
}
}

func (mr *ManagedResource) IsNamespaced() bool {
return true
}
Loading
Loading