mirror of
https://github.com/certimate-go/certimate.git
synced 2026-09-24 23:10:13 +08:00
refactor: re-implement kubernetes services with rest client, to lightweight package size
This commit is contained in:
@@ -113,7 +113,6 @@ require (
|
||||
github.com/fxamacker/cbor/v2 v2.9.1 // indirect
|
||||
github.com/go-acme/tencentclouddnspod v1.3.24 // indirect
|
||||
github.com/go-logr/logr v1.4.3 // indirect
|
||||
github.com/go-openapi/jsonpointer v0.22.5 // indirect
|
||||
github.com/go-openapi/jsonreference v0.21.5 // indirect
|
||||
github.com/go-openapi/swag v0.25.5 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
@@ -152,7 +151,6 @@ require (
|
||||
go.uber.org/ratelimit v0.3.1 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.4 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.4 // indirect
|
||||
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
|
||||
gopkg.in/inf.v0 v0.9.1 // indirect
|
||||
gopkg.in/ns1/ns1-go.v2 v2.17.2 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
@@ -209,17 +207,6 @@ require (
|
||||
github.com/go-acme/tencentedgdeone v1.3.38 // indirect
|
||||
github.com/go-jose/go-jose/v4 v4.1.4 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/go-openapi/swag/cmdutils v0.25.5 // indirect
|
||||
github.com/go-openapi/swag/conv v0.25.5 // indirect
|
||||
github.com/go-openapi/swag/fileutils v0.25.5 // indirect
|
||||
github.com/go-openapi/swag/jsonname v0.25.5 // indirect
|
||||
github.com/go-openapi/swag/jsonutils v0.25.5 // indirect
|
||||
github.com/go-openapi/swag/loading v0.25.5 // indirect
|
||||
github.com/go-openapi/swag/mangling v0.25.5 // indirect
|
||||
github.com/go-openapi/swag/netutils v0.25.5 // indirect
|
||||
github.com/go-openapi/swag/stringutils v0.25.5 // indirect
|
||||
github.com/go-openapi/swag/typeutils v0.25.5 // indirect
|
||||
github.com/go-openapi/swag/yamlutils v0.25.5 // indirect
|
||||
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 // indirect
|
||||
github.com/go-test/deep v1.1.1 // indirect
|
||||
github.com/google/s2a-go v0.1.9 // indirect
|
||||
|
||||
@@ -395,8 +395,6 @@ github.com/go-openapi/swag/jsonname v0.25.5 h1:8p150i44rv/Drip4vWI3kGi9+4W9TdI3U
|
||||
github.com/go-openapi/swag/jsonname v0.25.5/go.mod h1:jNqqikyiAK56uS7n8sLkdaNY/uq6+D2m2LANat09pKU=
|
||||
github.com/go-openapi/swag/jsonutils v0.25.5 h1:XUZF8awQr75MXeC+/iaw5usY/iM7nXPDwdG3Jbl9vYo=
|
||||
github.com/go-openapi/swag/jsonutils v0.25.5/go.mod h1:48FXUaz8YsDAA9s5AnaUvAmry1UcLcNVWUjY42XkrN4=
|
||||
github.com/go-openapi/swag/jsonutils/fixtures_test v0.25.5 h1:SX6sE4FrGb4sEnnxbFL/25yZBb5Hcg1inLeErd86Y1U=
|
||||
github.com/go-openapi/swag/jsonutils/fixtures_test v0.25.5/go.mod h1:/2KvOTrKWjVA5Xli3DZWdMCZDzz3uV/T7bXwrKWPquo=
|
||||
github.com/go-openapi/swag/loading v0.25.5 h1:odQ/umlIZ1ZVRteI6ckSrvP6e2w9UTF5qgNdemJHjuU=
|
||||
github.com/go-openapi/swag/loading v0.25.5/go.mod h1:I8A8RaaQ4DApxhPSWLNYWh9NvmX2YKMoB9nwvv6oW6g=
|
||||
github.com/go-openapi/swag/mangling v0.25.5 h1:hyrnvbQRS7vKePQPHHDso+k6CGn5ZBs5232UqWZmJZw=
|
||||
@@ -409,10 +407,6 @@ github.com/go-openapi/swag/typeutils v0.25.5 h1:EFJ+PCga2HfHGdo8s8VJXEVbeXRCYwzz
|
||||
github.com/go-openapi/swag/typeutils v0.25.5/go.mod h1:itmFmScAYE1bSD8C4rS0W+0InZUBrB2xSPbWt6DLGuc=
|
||||
github.com/go-openapi/swag/yamlutils v0.25.5 h1:kASCIS+oIeoc55j28T4o8KwlV2S4ZLPT6G0iq2SSbVQ=
|
||||
github.com/go-openapi/swag/yamlutils v0.25.5/go.mod h1:Gek1/SjjfbYvM+Iq4QGwa/2lEXde9n2j4a3wI3pNuOQ=
|
||||
github.com/go-openapi/testify/enable/yaml/v2 v2.4.0 h1:7SgOMTvJkM8yWrQlU8Jm18VeDPuAvB/xWrdxFJkoFag=
|
||||
github.com/go-openapi/testify/enable/yaml/v2 v2.4.0/go.mod h1:14iV8jyyQlinc9StD7w1xVPW3CO3q1Gj04Jy//Kw4VM=
|
||||
github.com/go-openapi/testify/v2 v2.4.0 h1:8nsPrHVCWkQ4p8h1EsRVymA2XABB4OT40gcvAu+voFM=
|
||||
github.com/go-openapi/testify/v2 v2.4.0/go.mod h1:HCPmvFFnheKK2BuwSA0TbbdxJ3I16pjwMkYkP4Ywn54=
|
||||
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
|
||||
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
|
||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
|
||||
@@ -12,8 +12,6 @@ import (
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
|
||||
typedcore "github.com/certimate-go/certimate/pkg/sdk3rd-trimmed/k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
|
||||
"github.com/certimate-go/certimate/pkg/core"
|
||||
xcert "github.com/certimate-go/certimate/pkg/utils/cert"
|
||||
)
|
||||
@@ -104,10 +102,15 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
|
||||
}
|
||||
|
||||
// 获取 Secret 实例
|
||||
var secretPayload *k8score.Secret
|
||||
var secretIsNew bool
|
||||
secretPayload, err = client.Secrets(d.config.Namespace).Get(ctx, d.config.SecretName, meta.GetOptions{})
|
||||
if err != nil {
|
||||
secretPayload := &k8score.Secret{}
|
||||
secretIsNew := false
|
||||
secretGetResp := client.Get().
|
||||
Namespace(d.config.Namespace).
|
||||
Resource("secrets").
|
||||
Name(d.config.SecretName).
|
||||
VersionedParams(&meta.GetOptions{}, meta.ParameterCodec).
|
||||
Do(ctx)
|
||||
if err := secretGetResp.Error(); err != nil {
|
||||
if !k8serrs.IsNotFound(err) {
|
||||
return nil, fmt.Errorf("failed to get kubernetes secret: %w", err)
|
||||
}
|
||||
@@ -123,7 +126,10 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
|
||||
},
|
||||
}
|
||||
secretIsNew = true
|
||||
} else if err := secretGetResp.Into(secretPayload); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse kubernetes secret: %w", err)
|
||||
}
|
||||
d.logger.Debug("kubernetes operate 'Secrets.Get'", slog.String("namespace", d.config.Namespace), slog.Any("secret", d.config.SecretName))
|
||||
|
||||
// 生成 Secret 注解和标签
|
||||
secretAnnotations := map[string]string{
|
||||
@@ -179,15 +185,27 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
|
||||
|
||||
// 创建或更新 Secret 实例
|
||||
if secretIsNew {
|
||||
secretPayload, err = client.Secrets(d.config.Namespace).Create(ctx, secretPayload, meta.CreateOptions{})
|
||||
d.logger.Debug("kubernetes operate 'Secrets.Create'", slog.String("namespace", d.config.Namespace), slog.Any("secret", secretPayload))
|
||||
if err != nil {
|
||||
secretPostResp := client.Post().
|
||||
Namespace(d.config.Namespace).
|
||||
Resource("secrets").
|
||||
Name(d.config.SecretName).
|
||||
VersionedParams(&meta.GetOptions{}, meta.ParameterCodec).
|
||||
Body(secretPayload).
|
||||
Do(ctx)
|
||||
d.logger.Debug("kubernetes operate 'Secrets.Post'", slog.String("namespace", d.config.Namespace), slog.Any("secret", d.config.SecretName))
|
||||
if err := secretPostResp.Error(); err != nil {
|
||||
return nil, fmt.Errorf("failed to create kubernetes secret: %w", err)
|
||||
}
|
||||
} else {
|
||||
secretPayload, err = client.Secrets(d.config.Namespace).Update(ctx, secretPayload, meta.UpdateOptions{})
|
||||
d.logger.Debug("kubernetes operate 'Secrets.Update'", slog.String("namespace", d.config.Namespace), slog.Any("secret", secretPayload))
|
||||
if err != nil {
|
||||
secretPutResp := client.Put().
|
||||
Namespace(d.config.Namespace).
|
||||
Resource("secrets").
|
||||
Name(d.config.SecretName).
|
||||
VersionedParams(&meta.GetOptions{}, meta.ParameterCodec).
|
||||
Body(secretPayload).
|
||||
Do(ctx)
|
||||
d.logger.Debug("kubernetes operate 'Secrets.Put'", slog.String("namespace", d.config.Namespace), slog.Any("secret", d.config.SecretName))
|
||||
if err := secretPutResp.Error(); err != nil {
|
||||
return nil, fmt.Errorf("failed to update kubernetes secret: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -195,7 +213,7 @@ func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*Dep
|
||||
return &DeployResult{}, nil
|
||||
}
|
||||
|
||||
func createK8sClient(kubeConfig string) (*typedcore.CoreV1Client, error) {
|
||||
func createK8sClient(kubeConfig string) (*rest.RESTClient, error) {
|
||||
var config *rest.Config
|
||||
var err error
|
||||
if kubeConfig == "" {
|
||||
@@ -211,7 +229,7 @@ func createK8sClient(kubeConfig string) (*typedcore.CoreV1Client, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client, err := typedcore.NewForConfig(config)
|
||||
client, err := rest.RESTClientFor(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
http "net/http"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
scheme "k8s.io/client-go/kubernetes/scheme"
|
||||
rest "k8s.io/client-go/rest"
|
||||
)
|
||||
|
||||
type CoreV1Interface interface {
|
||||
RESTClient() rest.Interface
|
||||
SecretsGetter
|
||||
}
|
||||
|
||||
type CoreV1Client struct {
|
||||
restClient rest.Interface
|
||||
}
|
||||
|
||||
func (c *CoreV1Client) Secrets(namespace string) SecretInterface {
|
||||
return newSecrets(c, namespace)
|
||||
}
|
||||
|
||||
func NewForConfig(c *rest.Config) (*CoreV1Client, error) {
|
||||
config := *c
|
||||
setConfigDefaults(&config)
|
||||
httpClient, err := rest.HTTPClientFor(&config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewForConfigAndClient(&config, httpClient)
|
||||
}
|
||||
|
||||
func NewForConfigAndClient(c *rest.Config, h *http.Client) (*CoreV1Client, error) {
|
||||
config := *c
|
||||
setConfigDefaults(&config)
|
||||
client, err := rest.RESTClientForConfigAndClient(&config, h)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &CoreV1Client{client}, nil
|
||||
}
|
||||
|
||||
func setConfigDefaults(config *rest.Config) {
|
||||
gv := corev1.SchemeGroupVersion
|
||||
config.GroupVersion = &gv
|
||||
config.APIPath = "/api"
|
||||
config.NegotiatedSerializer = rest.CodecFactoryForGeneratedClient(scheme.Scheme, scheme.Codecs).WithoutConversion()
|
||||
|
||||
if config.UserAgent == "" {
|
||||
config.UserAgent = rest.DefaultKubernetesUserAgent()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CoreV1Client) RESTClient() rest.Interface {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return c.restClient
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
)
|
||||
|
||||
type SecretExpansion = typedcorev1.SecretExpansion
|
||||
@@ -1,31 +0,0 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
applyconfigurationscorev1 "k8s.io/client-go/applyconfigurations/core/v1"
|
||||
gentype "k8s.io/client-go/gentype"
|
||||
scheme "k8s.io/client-go/kubernetes/scheme"
|
||||
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
)
|
||||
|
||||
type SecretsGetter = typedcorev1.SecretsGetter
|
||||
|
||||
type SecretInterface = typedcorev1.SecretInterface
|
||||
|
||||
type secrets struct {
|
||||
*gentype.ClientWithListAndApply[*corev1.Secret, *corev1.SecretList, *applyconfigurationscorev1.SecretApplyConfiguration]
|
||||
}
|
||||
|
||||
func newSecrets(c *CoreV1Client, namespace string) *secrets {
|
||||
return &secrets{
|
||||
gentype.NewClientWithListAndApply[*corev1.Secret, *corev1.SecretList, *applyconfigurationscorev1.SecretApplyConfiguration](
|
||||
"secrets",
|
||||
c.RESTClient(),
|
||||
scheme.ParameterCodec,
|
||||
namespace,
|
||||
func() *corev1.Secret { return &corev1.Secret{} },
|
||||
func() *corev1.SecretList { return &corev1.SecretList{} },
|
||||
gentype.PrefersProtobuf[*corev1.Secret](),
|
||||
),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user