Skip to content

Commit

Permalink
Register apprelease crd
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Oct 25, 2023
1 parent 791f447 commit 1438d4a
Show file tree
Hide file tree
Showing 7 changed files with 522 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
kmodules.xyz/resource-metadata v0.17.27-0.20231019125620-bf868a29e3f9
sigs.k8s.io/controller-runtime v0.13.1
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd
x-helm.dev/apimachinery v0.0.12
)

require (
Expand Down Expand Up @@ -120,7 +121,6 @@ require (
kmodules.xyz/resource-metrics v0.25.5 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
x-helm.dev/apimachinery v0.0.12 // indirect
)

replace github.com/Masterminds/sprig/v3 => github.com/gomodules/sprig/v3 v3.2.3-0.20220405051441-0a8a99bac1b8
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func main() {
klog.Fatalln(err)
}
s.Checkpoint()
err = resources.RegisterCRDs(restcfg)
if err != nil {
klog.Fatalln(err)
}

/*
mapper, err := apiutil.NewDiscoveryRESTMapper(restcfg)
Expand Down
43 changes: 43 additions & 0 deletions pkg/resources/helm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright AppsCode Inc. and Contributors.
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 resources

import (
"fmt"

"kmodules.xyz/client-go/apiextensions"

crd_cs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/client-go/rest"
driversapi "x-helm.dev/apimachinery/apis/drivers/v1alpha1"
)

func RegisterCRDs(restcfg *rest.Config) error {
// register AppRelease CRD
crds := []*apiextensions.CustomResourceDefinition{
driversapi.AppRelease{}.CustomResourceDefinition(),
}
crdClient, err := crd_cs.NewForConfig(restcfg)
if err != nil {
return fmt.Errorf("failed to create crd client, reason %v", err)
}
err = apiextensions.RegisterCRDs(crdClient, crds)
if err != nil {
return fmt.Errorf("failed to register appRelease crd, reason %v", err)
}
return nil
}
1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ sigs.k8s.io/yaml
# x-helm.dev/apimachinery v0.0.12
## explicit; go 1.19
x-helm.dev/apimachinery/apis/charts/v1alpha1
x-helm.dev/apimachinery/apis/drivers/v1alpha1
x-helm.dev/apimachinery/apis/releases/v1alpha1
x-helm.dev/apimachinery/apis/shared
x-helm.dev/apimachinery/crds
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime"
"regexp"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"kmodules.xyz/client-go/apiextensions"
"x-helm.dev/apimachinery/apis/shared"
"x-helm.dev/apimachinery/crds"
)

const (
ResourceKindAppRelease = "AppRelease"
ResourceAppRelease = "apprelease"
ResourceAppReleases = "appreleases"
)

// Descriptor defines the Metadata and informations about the AppRelease.
type Descriptor struct {
// Type is the type of the appRelease (e.g. WordPress, MySQL, Cassandra).
Type string `json:"type,omitempty"`

// Version is an optional version indicator for the AppRelease.
Version string `json:"version,omitempty"`

// Description is a brief string description of the AppRelease.
Description string `json:"description,omitempty"`

// Icons is an optional list of icons for an appRelease. Icon information includes the source, size,
// and mime type.
Icons []shared.ImageSpec `json:"icons,omitempty"`

// Maintainers is an optional list of maintainers of the appRelease. The maintainers in this list maintain the
// the source code, images, and package for the appRelease.
Maintainers []shared.ContactData `json:"maintainers,omitempty"`

// Owners is an optional list of the owners of the installed appRelease. The owners of the appRelease should be
// contacted in the event of a planned or unplanned disruption affecting the appRelease.
Owners []shared.ContactData `json:"owners,omitempty"`

// Keywords is an optional list of key words associated with the appRelease (e.g. MySQL, RDBMS, database).
Keywords []string `json:"keywords,omitempty"`

// Links are a list of descriptive URLs intended to be used to surface additional documentation, dashboards, etc.
Links []shared.Link `json:"links,omitempty"`

// Notes contain a human readable snippets intended as a quick start for the users of the AppRelease.
// CommonMark markdown syntax may be used for rich text representation.
Notes string `json:"notes,omitempty"`
}

// AppReleaseSpec defines the specification for an AppRelease.
type AppReleaseSpec struct {
// Descriptor regroups information and metadata about an appRelease.
Descriptor Descriptor `json:"descriptor,omitempty"`

// Release regroups information and metadata about a Helm release.
Release ReleaseInfo `json:"release,omitempty"`

// Components is a list of Kinds for AppRelease's components (e.g. Deployments, Pods, Services, CRDs). It
// can be used in conjunction with the AppRelease's Selector to list or watch the AppReleases components.
Components []metav1.GroupVersionKind `json:"components,omitempty"`

// Selector is a label query over kinds that created by the appRelease. It must match the component objects' labels.
// More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
Selector *metav1.LabelSelector `json:"selector,omitempty"`

Editor *metav1.GroupVersionResource `json:"editor,omitempty"`

// +optional
ResourceKeys []string `json:"resourceKeys,omitempty"`

// +optional
FormKeys []string `json:"formKeys,omitempty"`
}

type ReleaseInfo struct {
Name string `json:"name"`
Version string `json:"version,omitempty"`
Status string `json:"status,omitempty"`
FirstDeployed *metav1.Time `json:"firstDeployed,omitempty"`
LastDeployed *metav1.Time `json:"lastDeployed,omitempty"`
ModifiedAt *metav1.Time `json:"modified-at,omitempty"`
Form *runtime.RawExtension `json:"form,omitempty"`
}

// ComponentList is a generic status holder for the top level resource
type ComponentList struct {
// Object status array for all matching objects
Objects []ObjectStatus `json:"components,omitempty"`
}

// ObjectStatus is a generic status holder for objects
type ObjectStatus struct {
// Link to object
Link string `json:"link,omitempty"`
// Name of object
Name string `json:"name,omitempty"`
// Kind of object
Kind string `json:"kind,omitempty"`
// Object group
Group string `json:"group,omitempty"`
// Status. Values: InProgress, Ready, Unknown
Status string `json:"status,omitempty"`
}

// AppReleaseStatus defines controller's the observed state of AppRelease
type AppReleaseStatus struct {
// ObservedGeneration is the most recent generation observed. It corresponds to the
// Object's generation, which is updated on mutation by the API Server.
// +optional
ObservedGeneration int64 `json:"observedGeneration,omitempty" protobuf:"varint,1,opt,name=observedGeneration"`
// Conditions represents the latest state of the object
// +optional
// +patchMergeKey=type
// +patchStrategy=merge
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,10,rep,name=conditions"`
// Resources embeds a list of object statuses
// +optional
ComponentList `json:",inline,omitempty"`
// ComponentsReady: status of the components in the format ready/total
// +optional
ComponentsReady string `json:"componentsReady,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Type",type=string,description="The type of the appRelease",JSONPath=`.spec.descriptor.type`,priority=0
// +kubebuilder:printcolumn:name="Version",type=string,description="The creation date",JSONPath=`.spec.descriptor.version`,priority=0
// +kubebuilder:printcolumn:name="Age",type=date,description="The creation date",JSONPath=`.metadata.creationTimestamp`,priority=0

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

Spec AppReleaseSpec `json:"spec,omitempty"`
Status AppReleaseStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&AppRelease{}, &AppReleaseList{})
}

func (_ AppRelease) CustomResourceDefinition() *apiextensions.CustomResourceDefinition {
return crds.MustCustomResourceDefinition(GroupVersion.WithResource(ResourceAppReleases))
}

// StripVersion the version part of gv
func StripVersion(gv string) string {
if gv == "" {
return gv
}

re := regexp.MustCompile(`^[vV][0-9].*`)
// If it begins with only version, (group is nil), return empty string which maps to core group
if re.MatchString(gv) {
return ""
}

return strings.Split(gv, "/")[0]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2023.
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 drivers v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=drivers.x-helm.dev
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: "drivers.x-helm.dev", 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
)
Loading

0 comments on commit 1438d4a

Please sign in to comment.