forked from projectcalico/calicoctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
125 lines (108 loc) · 4.39 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
// Copyright (c) 2016 Tigera, Inc. All rights reserved.
// 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 commands
import (
"fmt"
"os"
"strings"
"github.com/docopt/docopt-go"
log "github.com/sirupsen/logrus"
"github.com/projectcalico/calicoctl/calicoctl/commands/constants"
)
func Create(args []string) {
doc := constants.DatastoreIntro + `Usage:
calicoctl create --filename=<FILENAME> [--skip-exists] [--config=<CONFIG>] [--namespace=<NS>]
Examples:
# Create a policy using the data in policy.yaml.
calicoctl create -f ./policy.yaml
# Create a policy based on the JSON passed into stdin.
cat policy.json | calicoctl create -f -
Options:
-h --help Show this screen.
-f --filename=<FILENAME> Filename to use to create the resource. If set to
"-" loads from stdin.
--skip-exists Skip over and treat as successful any attempts to
create an entry that already exists.
-c --config=<CONFIG> Path to the file containing connection
configuration in YAML or JSON format.
[default: ` + constants.DefaultConfigPath + `]
-n --namespace=<NS> Namespace of the resource.
Only applicable to NetworkPolicy and WorkloadEndpoint.
Uses the default namespace if not specified.
Description:
The create command is used to create a set of resources by filename or stdin.
JSON and YAML formats are accepted.
Valid resource types are:
* bgpConfiguration
* bgpPeer
* felixConfiguration
* globalNetworkPolicy
* hostEndpoint
* ipPool
* networkPolicy
* node
* profile
* workloadEndpoint
Attempting to create a resource that already exists is treated as a
terminating error unless the --skip-exists flag is set. If this flag is set,
resources that already exist are skipped.
The output of the command indicates how many resources were successfully
created, and the error reason if an error occurred. If the --skip-exists
flag is set then skipped resources are included in the success count.
The resources are created in the order they are specified. In the event of a
failure creating a specific resource it is possible to work out which
resource failed based on the number of resources successfully created.
`
parsedArgs, err := docopt.Parse(doc, args, true, "", false, false)
if err != nil {
fmt.Printf("Invalid option: 'calicoctl %s'. Use flag '--help' to read about a specific subcommand.\n", strings.Join(args, " "))
os.Exit(1)
}
if len(parsedArgs) == 0 {
return
}
results := executeConfigCommand(parsedArgs, actionCreate)
log.Infof("results: %+v", results)
if results.fileInvalid {
fmt.Printf("Failed to execute command: %v\n", results.err)
os.Exit(1)
} else if results.numHandled == 0 {
if results.numResources == 0 {
fmt.Printf("No resources specified in file\n")
} else if results.numResources == 1 {
fmt.Printf("Failed to create '%s' resource: %v\n", results.singleKind, results.err)
} else if results.singleKind != "" {
fmt.Printf("Failed to create any '%s' resources: %v\n", results.singleKind, results.err)
} else {
fmt.Printf("Failed to create any resources: %v\n", results.err)
}
os.Exit(1)
} else if results.err == nil {
if results.singleKind != "" {
fmt.Printf("Successfully created %d '%s' resource(s)\n", results.numHandled, results.singleKind)
} else {
fmt.Printf("Successfully created %d resource(s)\n", results.numHandled)
}
} else {
fmt.Printf("Partial success: ")
if results.singleKind != "" {
fmt.Printf("created the first %d out of %d '%s' resources:\n",
results.numHandled, results.numResources, results.singleKind)
} else {
fmt.Printf("created the first %d out of %d resources:\n",
results.numHandled, results.numResources)
}
fmt.Printf("Hit error: %v\n", results.err)
os.Exit(1)
}
}