Skip to content

Commit

Permalink
Merge pull request #256 from gianlucam76/v1beta1
Browse files Browse the repository at this point in the history
Move to v1beta1
  • Loading branch information
gianlucam76 authored Jun 24, 2024
2 parents 1673e77 + e70b9f9 commit 51d635f
Show file tree
Hide file tree
Showing 85 changed files with 2,610 additions and 421 deletions.
36 changes: 33 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ KUSTOMIZE_PKG := sigs.k8s.io/kustomize/kustomize/v5
$(KUSTOMIZE): # Build kustomize from tools folder.
CGO_ENABLED=0 GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(KUSTOMIZE_PKG) $(KUSTOMIZE_BIN) $(KUSTOMIZE_VER)

CONVERSION_GEN_VER := v0.30.0
CONVERSION_GEN_BIN := conversion-gen
# We are intentionally using the binary without version suffix, to avoid the version
# in generated files.
CONVERSION_GEN := $(abspath $(TOOLS_BIN_DIR)/$(CONVERSION_GEN_BIN))
CONVERSION_GEN_PKG := k8s.io/code-generator/cmd/conversion-gen

.PHONY: $(CONVERSION_GEN_BIN)
$(CONVERSION_GEN_BIN): $(CONVERSION_GEN) ## Build a local copy of conversion-gen.

## We are forcing a rebuilt of conversion-gen via PHONY so that we're always using an up-to-date version.
## We can't use a versioned name for the binary, because that would be reflected in generated files.
.PHONY: $(CONVERSION_GEN)
$(CONVERSION_GEN): # Build conversion-gen from tools folder.
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(CONVERSION_GEN_PKG) $(CONVERSION_GEN_BIN) $(CONVERSION_GEN_VER)

SETUP_ENVTEST_VER := v0.0.0-20240522175850-2e9781e9fc60
SETUP_ENVTEST_BIN := setup-envtest
SETUP_ENVTEST := $(abspath $(TOOLS_BIN_DIR)/$(SETUP_ENVTEST_BIN)-$(SETUP_ENVTEST_VER))
Expand Down Expand Up @@ -83,7 +99,7 @@ help: ## Display this help.
##@ Tools

.PHONY: tools
tools: $(GOLANGCI_LINT) $(GOIMPORTS) $(GINKGO) $(KUBECTL) $(SETUP_ENVTEST) $(CONTROLLER_GEN) ## build all tools
tools: $(GOLANGCI_LINT) $(GOIMPORTS) $(GINKGO) $(KUBECTL) $(SETUP_ENVTEST) $(CONTROLLER_GEN) $(CONVERSION_GEN) ## build all tools

.PHONY: clean
clean: ## Remove all built tools
Expand All @@ -100,11 +116,25 @@ generate-modules: ## Run go mod tidy to ensure modules are up to date
generate: ## Run all generate-manifests-*, generate-go-deepcopy-*
$(MAKE) generate-modules generate-manifests generate-go-deepcopy
cp k8s/sveltosctl.yaml manifest/manifest.yaml
cat config/crd/bases/utils.projectsveltos.io_snapshots.yaml >> manifest/manifest.yaml
cat config/crd/bases/utils.projectsveltos.io_techsupports.yaml >> manifest/manifest.yaml
mkdir tmp; $(KUSTOMIZE) build config/default -o tmp
cat tmp/apiextensions.k8s.io_v1_customresourcedefinition_snapshots.utils.projectsveltos.io.yaml >> manifest/manifest.yaml
cat tmp/apiextensions.k8s.io_v1_customresourcedefinition_techsupports.utils.projectsveltos.io.yaml >> manifest/manifest.yaml
rm -rf tmp
MANIFEST_IMG=$(SVELTOSCTL_IMG) MANIFEST_TAG=$(TAG) $(MAKE) set-manifest-image
$(MAKE) fmt

.PHONY: generate-go-conversions
generate-go-conversions: $(CONVERSION_GEN) ## Generate conversions go code for utils.projectsveltos.io api
$(MAKE) clean-generated-conversions SRC_DIRS="./api/v1alpha1"
$(CONVERSION_GEN) \
--output-file=zz_generated.conversion.go \
--go-header-file=./hack/boilerplate.generatego.txt \
./api/v1alpha1

.PHONY: clean-generated-conversions
clean-generated-conversions: ## Remove files generated by conversion-gen from the mentioned dirs. Example SRC_DIRS="./api/v1alpha4"
(IFS=','; for i in $(SRC_DIRS); do find $$i -type f -name 'zz_generated.conversion*' -exec rm -f {} \;; done)

set-manifest-image:
sed -i'' -e 's@image: .*@image: '"${MANIFEST_IMG}:$(MANIFEST_TAG)"'@' ./manifest/manifest.yaml

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ Define a Techsupport instance, following for instance will collect a techsupport
from all managed clusters matching cluster selectors __env=fv__

```
apiVersion: utils.projectsveltos.io/v1alpha1
apiVersion: utils.projectsveltos.io/v1beta1
kind: Techsupport
metadata:
name: hourly
Expand Down Expand Up @@ -311,7 +311,7 @@ A snapshot allows an administrator to perform the following tasks:
Define a Snapshot instance, following for instance will take a snaphost every hour.

```
apiVersion: utils.projectsveltos.io/v1alpha1
apiVersion: utils.projectsveltos.io/v1beta1
kind: Snapshot
metadata:
name: hourly
Expand Down
20 changes: 20 additions & 0 deletions api/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2024. projectsveltos.io. All rights reserved.
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

// +k8s:conversion-gen=github.com/projectsveltos/sveltosctl/api/v1beta1
// +k8s:deepcopy-gen=package
2 changes: 2 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ var (

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme

localSchemeBuilder = SchemeBuilder.SchemeBuilder
)
92 changes: 92 additions & 0 deletions api/v1alpha1/techsupport_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2024. projectsveltos.io. All rights reserved.
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"
apimachineryconversion "k8s.io/apimachinery/pkg/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"

libsveltosv1alpha1 "github.com/projectsveltos/libsveltos/api/v1alpha1"
libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
utilsv1beta1 "github.com/projectsveltos/sveltosctl/api/v1beta1"
)

// ConvertTo converts v1alpha1 to the Hub version (v1beta1).
func (src *Techsupport) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*utilsv1beta1.Techsupport)
err := Convert_v1alpha1_Techsupport_To_v1beta1_Techsupport(src, dst, nil)
if err != nil {
return err
}

if src.Spec.ClusterSelector == "" {
dst.Spec.ClusterSelector.LabelSelector = metav1.LabelSelector{}
}

return nil
}

// ConvertFrom converts from the Hub version (v1beta1) to this v1alpha1.
func (dst *Techsupport) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*utilsv1beta1.Techsupport)
err := Convert_v1beta1_Techsupport_To_v1alpha1_Techsupport(src, dst, nil)
if err != nil {
return err
}

if src.Spec.ClusterSelector.MatchLabels == nil {
dst.Spec.ClusterSelector = ""
}

return nil
}

func Convert_v1alpha1_TechsupportSpec_To_v1beta1_TechsupportSpec(srcSpec *TechsupportSpec, dstSpec *utilsv1beta1.TechsupportSpec,
scope apimachineryconversion.Scope) error {

if err := autoConvert_v1alpha1_TechsupportSpec_To_v1beta1_TechsupportSpec(srcSpec, dstSpec, nil); err != nil {
return err
}

labelSelector, err := metav1.ParseToLabelSelector(string(srcSpec.ClusterSelector))
if err != nil {
return fmt.Errorf("error converting labels.Selector to metav1.Selector: %w", err)
}
dstSpec.ClusterSelector = libsveltosv1beta1.Selector{LabelSelector: *labelSelector}

return nil
}

func Convert_v1beta1_TechsupportSpec_To_v1alpha1_TechsupportSpec(srcSpec *utilsv1beta1.TechsupportSpec, dstSpec *TechsupportSpec,
scope apimachineryconversion.Scope) error {

if err := autoConvert_v1beta1_TechsupportSpec_To_v1alpha1_TechsupportSpec(srcSpec, dstSpec, nil); err != nil {
return err
}

labelSelector, err := srcSpec.ClusterSelector.ToSelector()
if err != nil {
return fmt.Errorf("failed to convert : %w", err)
}

dstSpec.ClusterSelector = libsveltosv1alpha1.Selector(labelSelector.String())

return nil
}
Loading

0 comments on commit 51d635f

Please sign in to comment.