Skip to content

Commit

Permalink
Handle options and skip issuer kinds print
Browse files Browse the repository at this point in the history
  • Loading branch information
charlieegan3 committed Nov 18, 2022
1 parent 63704d2 commit e0bd753
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
2 changes: 0 additions & 2 deletions internal/command/clusters/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ func Backup(run types.RunFunc, kubeConfigPath *string) *cobra.Command {
}
allIssuersString := allIssuers.String()

fmt.Println(allIssuersString)

flags := cmd.PersistentFlags()
flags.BoolVar(&formatResources, "format-resources", true, "if set, will remove some fields from resources such as status and metadata to allow them to be cleanly applied later")
flags.StringVar(&outputFormat, "format", "yaml", "output format, one of: yaml, json")
Expand Down
94 changes: 50 additions & 44 deletions internal/kubernetes/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,63 +100,69 @@ func FetchClusterBackup(ctx context.Context, opts ClusterBackupOptions) (*Cluste
}

// fetch all configured issuers and external issuers
issuers, err := fetchAllIssuers(ctx, opts.RestConfig, dropFields)
if err != nil {
return &ClusterBackup{}, fmt.Errorf("failed to backup issuers: %w", err)
if opts.IncludeIssuers {
issuers, err := fetchAllIssuers(ctx, opts.RestConfig, dropFields)
if err != nil {
return &ClusterBackup{}, fmt.Errorf("failed to backup issuers: %w", err)
}
clusterBackup = append(clusterBackup, issuers...)
}
clusterBackup = append(clusterBackup, issuers...)

// fetch certifcates
certificateClient, err := clients.NewCertificateClient(opts.RestConfig)
if err != nil {
return &ClusterBackup{}, fmt.Errorf("failed to create client for certificates: %w", err)
}
if opts.IncludeCertificates {
certificateClient, err := clients.NewCertificateClient(opts.RestConfig)
if err != nil {
return &ClusterBackup{}, fmt.Errorf("failed to create client for certificates: %w", err)
}

var certificates v1certmanager.CertificateList
err = certificateClient.List(
ctx,
&clients.GenericRequestOptions{DropFields: dropFields},
&certificates,
)
if err != nil {
return &ClusterBackup{}, fmt.Errorf("failed to list certificates: %w", err)
}
for _, c := range certificates.Items {
// we do not include ingress certs, skip them
skip := false
if len(c.OwnerReferences) > 0 {
for _, owner := range c.OwnerReferences {
if owner.Kind == "Ingress" {
fmt.Fprintf(os.Stderr, "skipping ingress-shim managed certificate %s/%s\n", c.Namespace, c.Name)
skip = true
break
var certificates v1certmanager.CertificateList
err = certificateClient.List(
ctx,
&clients.GenericRequestOptions{DropFields: dropFields},
&certificates,
)
if err != nil {
return &ClusterBackup{}, fmt.Errorf("failed to list certificates: %w", err)
}
for _, c := range certificates.Items {
// we do not include ingress certs, skip them
skip := false
if len(c.OwnerReferences) > 0 {
for _, owner := range c.OwnerReferences {
if owner.Kind == "Ingress" {
fmt.Fprintf(os.Stderr, "skipping ingress-shim managed certificate %s/%s\n", c.Namespace, c.Name)
skip = true
break
}
}
}
}
if !skip {
clusterBackup = append(clusterBackup, c)
if !skip {
clusterBackup = append(clusterBackup, c)
}
}
}

// fetch certificate request policies
// Note: this back up data is not used in the migration to an operator managed installation.
// These resourcse are only included for disaster recovery purposes.
certificateRequestPolicyClient, err := clients.NewCertificateRequestPolicyClient(opts.RestConfig)
if err != nil {
return &ClusterBackup{}, fmt.Errorf("failed to create client for certificate request policies: %w", err)
}
if opts.IncludeCertificateRequestPolicies {
certificateRequestPolicyClient, err := clients.NewCertificateRequestPolicyClient(opts.RestConfig)
if err != nil {
return &ClusterBackup{}, fmt.Errorf("failed to create client for certificate request policies: %w", err)
}

var certificateRequestPolicies v1alpha1approverpolicy.CertificateRequestPolicyList
err = certificateRequestPolicyClient.List(
ctx,
&clients.GenericRequestOptions{DropFields: dropFields},
&certificateRequestPolicies,
)
if err != nil {
return &ClusterBackup{}, fmt.Errorf("failed to list certificate request policies: %w", err)
}
for _, p := range certificateRequestPolicies.Items {
clusterBackup = append(clusterBackup, p)
var certificateRequestPolicies v1alpha1approverpolicy.CertificateRequestPolicyList
err = certificateRequestPolicyClient.List(
ctx,
&clients.GenericRequestOptions{DropFields: dropFields},
&certificateRequestPolicies,
)
if err != nil {
return &ClusterBackup{}, fmt.Errorf("failed to list certificate request policies: %w", err)
}
for _, p := range certificateRequestPolicies.Items {
clusterBackup = append(clusterBackup, p)
}
}

return &clusterBackup, nil
Expand Down

0 comments on commit e0bd753

Please sign in to comment.