Added new scheme to use for ctl commands

Signed-off-by: Haoxiang Zhou <haoxiang.zhou@jetstack.io>
This commit is contained in:
Haoxiang Zhou 2020-06-23 13:48:19 +01:00
parent 7e4fa1ab13
commit 24404aa1eb
5 changed files with 94 additions and 22 deletions

View File

@ -74,6 +74,7 @@ filegroup(
"//pkg/client/listers/certmanager/v1alpha2:all-srcs",
"//pkg/client/listers/certmanager/v1alpha3:all-srcs",
"//pkg/controller:all-srcs",
"//pkg/ctl:all-srcs",
"//pkg/feature:all-srcs",
"//pkg/internal:all-srcs",
"//pkg/issuer:all-srcs",

View File

@ -7,19 +7,16 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/certmanager/v1alpha2:go_default_library",
"//pkg/webhook:go_default_library",
"//pkg/ctl:go_default_library",
"@com_github_spf13_cobra//:go_default_library",
"@io_k8s_apimachinery//pkg/apis/meta/internalversion:go_default_library",
"@io_k8s_apimachinery//pkg/apis/meta/v1:go_default_library",
"@io_k8s_apimachinery//pkg/runtime:go_default_library",
"@io_k8s_apimachinery//pkg/runtime/schema:go_default_library",
"@io_k8s_apimachinery//pkg/runtime/serializer:go_default_library",
"@io_k8s_apimachinery//pkg/runtime/serializer/json:go_default_library",
"@io_k8s_apimachinery//pkg/util/runtime:go_default_library",
"@io_k8s_cli_runtime//pkg/genericclioptions:go_default_library",
"@io_k8s_cli_runtime//pkg/printers:go_default_library",
"@io_k8s_cli_runtime//pkg/resource:go_default_library",
"@io_k8s_client_go//kubernetes/scheme:go_default_library",
"@io_k8s_klog//:go_default_library",
"@io_k8s_kubectl//pkg/cmd/util:go_default_library",
"@io_k8s_kubectl//pkg/util/i18n:go_default_library",

View File

@ -21,23 +21,20 @@ import (
"github.com/spf13/cobra"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
apijson "k8s.io/apimachinery/pkg/runtime/serializer/json"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/cli-runtime/pkg/resource"
kscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
cmapiv1alpha2 "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2"
"github.com/jetstack/cert-manager/pkg/webhook"
"github.com/jetstack/cert-manager/pkg/ctl"
)
var (
@ -61,23 +58,11 @@ to change to output destination.`))
)
var (
// Use the webhook's scheme as it already has the internal cert-manager types,
// Use this scheme as it has the internal cert-manager types
// and their conversion functions registered.
// In future we may we want to consider creating a dedicated scheme used by
// the ctl tool.
scheme = webhook.Scheme
scheme = ctl.Scheme
)
func init() {
// This is used to add the List object type for outputting multiple input
// objects.
coreGroupVersion := schema.GroupVersion{Group: "", Version: runtime.APIVersionInternal}
scheme.AddKnownTypes(coreGroupVersion, &metainternalversion.List{})
metav1.AddToGroupVersion(scheme, schema.GroupVersion{Version: "v1"})
utilruntime.Must(kscheme.AddToScheme(scheme))
}
// Options is a struct to support convert command
type Options struct {
PrintFlags *genericclioptions.PrintFlags

33
pkg/ctl/BUILD.bazel Normal file
View File

@ -0,0 +1,33 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["scheme.go"],
importpath = "github.com/jetstack/cert-manager/pkg/ctl",
visibility = ["//visibility:public"],
deps = [
"//pkg/internal/apis/acme/install:go_default_library",
"//pkg/internal/apis/certmanager/install:go_default_library",
"//pkg/internal/apis/meta/install:go_default_library",
"@io_k8s_apimachinery//pkg/apis/meta/internalversion:go_default_library",
"@io_k8s_apimachinery//pkg/apis/meta/v1:go_default_library",
"@io_k8s_apimachinery//pkg/runtime:go_default_library",
"@io_k8s_apimachinery//pkg/runtime/schema:go_default_library",
"@io_k8s_apimachinery//pkg/util/runtime:go_default_library",
"@io_k8s_client_go//kubernetes/scheme:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

56
pkg/ctl/scheme.go Normal file
View File

@ -0,0 +1,56 @@
/*
Copyright 2020 The Jetstack cert-manager 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.
*/
// This package was created to have a scheme that has the internal cert-manager types,
// and their conversion functions as well as the List object type registered, which is needed for ctl command like
// `convert` or `create certificaterequest`.
package ctl
import (
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
kscheme "k8s.io/client-go/kubernetes/scheme"
acmeinstall "github.com/jetstack/cert-manager/pkg/internal/apis/acme/install"
cminstall "github.com/jetstack/cert-manager/pkg/internal/apis/certmanager/install"
metainstall "github.com/jetstack/cert-manager/pkg/internal/apis/meta/install"
)
// Define a Scheme that has all cert-manager API types registered, including
// the internal API version, defaulting functions and conversion functions for
// all external versions.
var (
// Scheme is a Kubernetes runtime.Scheme with all internal and external API
// versions for cert-manager types registered.
Scheme = runtime.NewScheme()
)
func init() {
cminstall.Install(Scheme)
acmeinstall.Install(Scheme)
metainstall.Install(Scheme)
// This is used to add the List object type
coreGroupVersion := schema.GroupVersion{Group: "", Version: runtime.APIVersionInternal}
Scheme.AddKnownTypes(coreGroupVersion, &metainternalversion.List{})
metav1.AddToGroupVersion(Scheme, schema.GroupVersion{Version: "v1"})
utilruntime.Must(kscheme.AddToScheme(Scheme))
}