-
Notifications
You must be signed in to change notification settings - Fork 12
/
create.go
146 lines (120 loc) · 3.96 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2021 - 2024 Crunchy Data Solutions, Inc.
//
// 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 cmd
import (
"context"
"fmt"
"strconv"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
"github.com/crunchydata/postgres-operator-client/internal"
"github.com/crunchydata/postgres-operator-client/internal/apis/postgres-operator.crunchydata.com/v1beta1"
)
// newCreateCommand returns the create subcommand of the PGO plugin.
// Subcommands of create will be use to create objects, backups, etc.
func newCreateCommand(config *internal.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create a resource",
Long: "Create a resource",
}
cmd.AddCommand(newCreateClusterCommand(config))
return cmd
}
// newCreateClusterCommand returns the create cluster subcommand.
// create cluster will take a cluster name as an argument and create a basic
// cluster using a kube client
func newCreateClusterCommand(config *internal.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "postgrescluster CLUSTER_NAME",
Aliases: []string{"postgresclusters"},
Short: "Create PostgresCluster with a given name",
Long: `Create basic PostgresCluster with a given name.
### RBAC Requirements
Resources Verbs
--------- -----
postgresclusters.postgres-operator.crunchydata.com [create]
### Usage`,
}
cmd.Args = cobra.ExactArgs(1)
var pgMajorVersion int
cmd.Flags().IntVar(&pgMajorVersion, "pg-major-version", 0, "Set the Postgres major version")
cobra.CheckErr(cmd.MarkFlagRequired("pg-major-version"))
cmd.Example = internal.FormatExample(`# Create a postgrescluster with Postgres 15
pgo create postgrescluster hippo --pg-major-version 15
### Example output
postgresclusters/hippo created`)
cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
clusterName := args[0]
namespace, err := config.Namespace()
if err != nil {
return err
}
mapping, client, err := v1beta1.NewPostgresClusterClient(config)
if err != nil {
return err
}
cluster, err := generateUnstructuredClusterYaml(clusterName, strconv.Itoa(pgMajorVersion))
if err != nil {
return err
}
u, err := client.
Namespace(namespace).
Create(ctx, cluster, config.Patch.CreateOptions(metav1.CreateOptions{}))
if err != nil {
return err
}
cmd.Printf("%s/%s created\n", mapping.Resource.Resource, u.GetName())
return nil
}
return cmd
}
// generateUnstructuredClusterYaml takes a name and returns a PostgresCluster
// in the unstructured format.
func generateUnstructuredClusterYaml(name, pgMajorVersion string) (*unstructured.Unstructured, error) {
var cluster unstructured.Unstructured
err := yaml.Unmarshal([]byte(fmt.Sprintf(`
apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
name: %s
spec:
postgresVersion: %s
instances:
- dataVolumeClaimSpec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: 1Gi
backups:
pgbackrest:
repos:
- name: repo1
volume:
volumeClaimSpec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: 1Gi
`, name, pgMajorVersion)), &cluster)
if err != nil {
return nil, err
}
return &cluster, nil
}