-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_repository.go
More file actions
160 lines (143 loc) · 4.8 KB
/
create_repository.go
File metadata and controls
160 lines (143 loc) · 4.8 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
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 pkg
import (
"context"
"fmt"
"stash.appscode.dev/apimachinery/apis/stash/v1alpha1"
"stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1alpha1/util"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/util/templates"
storage "kmodules.xyz/objectstore-api/api/v1"
)
var createRepositoryExample = templates.Examples(`
# Create a new repository
stash create repository --namespace=<namespace> <repository-name> [Flag]
stash create repository gcs-repo --namespace=demo --secret=gcs-secret --bucket=appscode-qa --prefix=/source/data --provider=gcs`)
type repositoryOption struct {
provider string
bucket string
endpoint string
maxConnections int64
secret string
prefix string
}
func NewCmdCreateRepository() *cobra.Command {
repoOpt := repositoryOption{}
cmd := &cobra.Command{
Use: "repository",
Short: `Create a new repository`,
Long: "Create a new Repository using Backend Credential",
Example: createRepositoryExample,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 || args[0] == "" {
return fmt.Errorf("repository name is not provided ")
}
repositoryName := args[0]
repository := newRepository(repoOpt, repositoryName, namespace)
repository, err := createRepository(repository, repository.ObjectMeta)
if err != nil {
return err
}
klog.Infof("Repository %s/%s has been created successfully.", repository.Namespace, repository.Name)
return err
},
}
cmd.Flags().StringVar(&repoOpt.provider, "provider", repoOpt.provider, "Backend provider (i.e. gcs, s3, azure etc)")
cmd.Flags().StringVar(&repoOpt.bucket, "bucket", repoOpt.bucket, "Name of the cloud bucket/container")
cmd.Flags().StringVar(&repoOpt.endpoint, "endpoint", repoOpt.endpoint, "Endpoint for s3/s3 compatible backend")
cmd.Flags().Int64Var(&repoOpt.maxConnections, "max-connections", repoOpt.maxConnections, "Specify maximum concurrent connections for GCS, Azure and B2 backend")
cmd.Flags().StringVar(&repoOpt.secret, "secret", repoOpt.secret, "Name of the Storage Secret")
cmd.Flags().StringVar(&repoOpt.prefix, "prefix", repoOpt.prefix, "Prefix denotes the directory inside the backend")
return cmd
}
func newRepository(opt repositoryOption, name string, namespace string) *v1alpha1.Repository {
repository := &v1alpha1.Repository{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: v1alpha1.RepositorySpec{
Backend: opt.getBackendInfo(),
},
}
return repository
}
// CreateOrPatch New Secret
func createRepository(repository *v1alpha1.Repository, meta metav1.ObjectMeta) (*v1alpha1.Repository, error) {
repository, _, err := util.CreateOrPatchRepository(
context.TODO(),
stashClient.StashV1alpha1(),
meta, func(in *v1alpha1.Repository) *v1alpha1.Repository {
in.Spec = repository.Spec
return in
},
metav1.PatchOptions{},
)
return repository, err
}
func (opt repositoryOption) getBackendInfo() storage.Backend {
var backend storage.Backend
switch opt.provider {
case storage.ProviderGCS:
backend = storage.Backend{
GCS: &storage.GCSSpec{
Bucket: opt.bucket,
Prefix: opt.prefix,
MaxConnections: opt.maxConnections,
},
}
case storage.ProviderAzure:
backend = storage.Backend{
Azure: &storage.AzureSpec{
Container: opt.bucket,
Prefix: opt.prefix,
MaxConnections: opt.maxConnections,
},
}
case storage.ProviderS3:
backend = storage.Backend{
S3: &storage.S3Spec{
Bucket: opt.bucket,
Prefix: opt.prefix,
Endpoint: opt.endpoint,
},
}
case storage.ProviderB2:
backend = storage.Backend{
B2: &storage.B2Spec{
Bucket: opt.bucket,
Prefix: opt.prefix,
MaxConnections: opt.maxConnections,
},
}
case storage.ProviderSwift:
backend = storage.Backend{
Swift: &storage.SwiftSpec{
Container: opt.bucket,
Prefix: opt.prefix,
},
}
case storage.ProviderRest:
backend = storage.Backend{
Rest: &storage.RestServerSpec{
URL: opt.endpoint,
},
}
}
backend.StorageSecretName = opt.secret
return backend
}