Skip to content

Commit

Permalink
refactor apply yaml by file into apply yalm by string and add ApplyYA…
Browse files Browse the repository at this point in the history
…MLStrings in client (istio#47247)

Signed-off-by: rokkiter <yongen.pan@daocloud.io>
  • Loading branch information
rokkiter authored Oct 11, 2023
1 parent 85dc0e2 commit 290b686
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 58 deletions.
29 changes: 1 addition & 28 deletions istioctl/pkg/multicluster/remote_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"fmt"
"io"
"os"
"strings"
"time"

Expand Down Expand Up @@ -421,7 +420,7 @@ func createServiceAccount(client kube.CLIClient, opt RemoteSecretOptions) error
}

// Apply the YAML to the cluster.
return applyYAML(client, yaml, opt.Namespace)
return client.ApplyYAMLContents(opt.Namespace, yaml)
}

func generateServiceAccountYAML(opt RemoteSecretOptions) (string, error) {
Expand Down Expand Up @@ -476,19 +475,6 @@ global:
return aggregateContent, nil
}

func applyYAML(client kube.CLIClient, yamlContent, ns string) error {
yamlFile, err := writeToTempFile(yamlContent)
if err != nil {
return fmt.Errorf("failed creating manifest file: %v", err)
}

// Apply the YAML to the cluster.
if err := client.ApplyYAMLFiles(ns, yamlFile); err != nil {
return fmt.Errorf("failed applying manifest %s: %v", yamlFile, err)
}
return nil
}

func createNamespaceIfNotExist(client kube.Client, ns string) error {
if _, err := client.Kube().CoreV1().Namespaces().Get(context.TODO(), ns, metav1.GetOptions{}); err != nil {
if _, err := client.Kube().CoreV1().Namespaces().Create(context.TODO(), &v1.Namespace{
Expand All @@ -502,19 +488,6 @@ func createNamespaceIfNotExist(client kube.Client, ns string) error {
return nil
}

func writeToTempFile(content string) (string, error) {
outFile, err := os.CreateTemp("", "remote-secret-manifest-*")
if err != nil {
return "", fmt.Errorf("failed creating temp file for manifest: %v", err)
}
defer func() { _ = outFile.Close() }()

if _, err := outFile.WriteString(content); err != nil {
return "", fmt.Errorf("failed writing manifest file: %v", err)
}
return outFile.Name(), nil
}

func getServerFromKubeconfig(client kube.CLIClient) (string, Warning, error) {
restCfg := client.RESTConfig()
if restCfg == nil {
Expand Down
31 changes: 1 addition & 30 deletions istioctl/pkg/tag/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"fmt"
"net/url"
"os"
"strings"

admitv1 "k8s.io/api/admissionregistration/v1"
Expand Down Expand Up @@ -193,7 +192,7 @@ func fixWhConfig(client kube.Client, whConfig *tagWebhookConfig) (*tagWebhookCon

// Create applies the given tag manifests.
func Create(client kube.CLIClient, manifests, ns string) error {
if err := applyYAML(client, manifests, ns); err != nil {
if err := client.ApplyYAMLContents(ns, manifests); err != nil {
return fmt.Errorf("failed to apply tag manifests to cluster: %v", err)
}
return nil
Expand Down Expand Up @@ -378,31 +377,3 @@ func tagWebhookConfigFromCanonicalWebhook(wh admitv1.MutatingWebhookConfiguratio
FailurePolicy: map[string]*admitv1.FailurePolicyType{},
}, nil
}

// applyYAML taken from remote_secret.go
func applyYAML(client kube.CLIClient, yamlContent, ns string) error {
yamlFile, err := writeToTempFile(yamlContent)
if err != nil {
return fmt.Errorf("failed creating manifest file: %w", err)
}

// Apply the YAML to the cluster.
if err := client.ApplyYAMLFiles(ns, yamlFile); err != nil {
return fmt.Errorf("failed applying manifest %s: %v", yamlFile, err)
}
return nil
}

// writeToTempFile taken from remote_secret.go
func writeToTempFile(content string) (string, error) {
outFile, err := os.CreateTemp("", "revision-tag-manifest-*")
if err != nil {
return "", fmt.Errorf("failed creating temp file for manifest: %w", err)
}
defer func() { _ = outFile.Close() }()

if _, err := outFile.WriteString(content); err != nil {
return "", fmt.Errorf("failed writing manifest file: %w", err)
}
return outFile.Name(), nil
}
17 changes: 17 additions & 0 deletions pkg/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ type CLIClient interface {
// ApplyYAMLFiles applies the resources in the given YAML files.
ApplyYAMLFiles(namespace string, yamlFiles ...string) error

// ApplyYAMLContents applies the resources in the given YAML strings.
ApplyYAMLContents(namespace string, yamls ...string) error

// ApplyYAMLFilesDryRun performs a dry run for applying the resource in the given YAML files
ApplyYAMLFilesDryRun(namespace string, yamlFiles ...string) error

Expand Down Expand Up @@ -924,6 +927,20 @@ func (c *client) ApplyYAMLFiles(namespace string, yamlFiles ...string) error {
return g.Wait()
}

func (c *client) ApplyYAMLContents(namespace string, yamls ...string) error {
g, _ := errgroup.WithContext(context.TODO())
for _, yaml := range yamls {
cfgs := yml.SplitString(yaml)
for _, cfg := range cfgs {
cfg := cfg
g.Go(func() error {
return c.ssapplyYAML(cfg, namespace, false)
})
}
}
return g.Wait()
}

func (c *client) ApplyYAMLFilesDryRun(namespace string, yamlFiles ...string) error {
g, _ := errgroup.WithContext(context.TODO())
for _, f := range removeEmptyFiles(yamlFiles) {
Expand Down

0 comments on commit 290b686

Please sign in to comment.