This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
manifest-diff.go
164 lines (145 loc) · 5.64 KB
/
manifest-diff.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2019 Istio Authors
//
// 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 mesh
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"istio.io/operator/pkg/compare"
"github.com/spf13/cobra"
"istio.io/operator/pkg/util"
)
// YAMLSuffix is the suffix of a YAML file.
const YAMLSuffix = ".yaml"
type manifestDiffArgs struct {
// compareDir indicates comparison between directory.
compareDir bool
// selectResources constrains the list of resources to compare to only the ones in this list, ignoring all others.
// The format of each list item is :: and the items are comma separated. The * character represents wildcard selection.
// e.g.
// Deployment:istio-system:* - compare all deployments in istio-system namespace
// Service:*:istio-pilot - compare Services called "istio-pilot" in all namespaces.
selectResources string
// ignoreResources ignores all listed items during comparison. It uses the same list format as selectResources.
ignoreResources string
// renameResources identifies renamed resources before comparison.
// The format of each renaming pair is A->B, all renaming pairs are comma separated.
// e.g. Service:*:istio-pilot->Service:*:istio-control - rename istio-pilot service into istio-control
renameResources string
}
func addManifestDiffFlags(cmd *cobra.Command, diffArgs *manifestDiffArgs) {
cmd.PersistentFlags().BoolVarP(&diffArgs.compareDir, "directory", "r",
false, "compare directory")
cmd.PersistentFlags().StringVar(&diffArgs.selectResources, "select", "::",
"selectResources constrains the list of resources to compare to only the ones in this list, ignoring all others.\n"+
"The format of each list item is \"::\" and the items are comma separated. The \"*\" character represents wildcard selection.\n"+
"e.g.\n"+
" Deployment:istio-system:* - compare all deployments in istio-system namespace\n"+
" Service:*:istio-pilot - compare Services called \"istio-pilot\" in all namespaces")
cmd.PersistentFlags().StringVar(&diffArgs.ignoreResources, "ignore", "",
"ignoreResources ignores all listed items during comparison. It uses the same list format as selectResources")
cmd.PersistentFlags().StringVar(&diffArgs.renameResources, "rename", "",
"renameResources identifies renamed resources before comparison.\n"+
"The format of each renaming pair is A->B, all renaming pairs are comma separated.\n"+
"e.g. Service:*:istio-pilot->Service:*:istio-control - rename istio-pilot service into istio-control")
}
func manifestDiffCmd(rootArgs *rootArgs, diffArgs *manifestDiffArgs) *cobra.Command {
cmd := &cobra.Command{
Use: "diff <file|dir> <file|dir>",
Short: "Compare manifests and generate diff",
Long: "The diff subcommand compares manifests from two files or directories.",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("diff requires two files or directories")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var err error
var equal bool
if diffArgs.compareDir {
equal, err = compareManifestsFromDirs(rootArgs, args[0], args[1], diffArgs.renameResources,
diffArgs.selectResources, diffArgs.ignoreResources)
if err != nil {
return err
}
if !equal {
os.Exit(1)
}
return nil
}
equal, err = compareManifestsFromFiles(rootArgs, args, diffArgs.renameResources,
diffArgs.selectResources, diffArgs.ignoreResources)
if err != nil {
return err
}
if !equal {
os.Exit(1)
}
return nil
}}
return cmd
}
//compareManifestsFromFiles compares two manifest files
func compareManifestsFromFiles(rootArgs *rootArgs, args []string,
renameResources, selectResources, ignoreResources string) (bool, error) {
initLogsOrExit(rootArgs)
a, err := ioutil.ReadFile(args[0])
if err != nil {
return false, fmt.Errorf("could not read %q: %v", args[0], err)
}
b, err := ioutil.ReadFile(args[1])
if err != nil {
return false, fmt.Errorf("could not read %q: %v", args[1], err)
}
diff, err := compare.ManifestDiffWithRenameSelectIgnore(string(a), string(b), renameResources, selectResources,
ignoreResources, rootArgs.verbose)
if err != nil {
return false, err
}
if diff != "" {
fmt.Printf("Differences in manifests are:\n%s\n", diff)
return false, nil
}
fmt.Println("Manifests are identical")
return true, nil
}
func yamlFileFilter(path string) bool {
return filepath.Ext(path) == YAMLSuffix
}
//compareManifestsFromDirs compares manifests from two directories
func compareManifestsFromDirs(rootArgs *rootArgs, dirName1, dirName2,
renameResources, selectResources, ignoreResources string) (bool, error) {
initLogsOrExit(rootArgs)
mf1, err := util.ReadFilesWithFilter(dirName1, yamlFileFilter)
if err != nil {
return false, err
}
mf2, err := util.ReadFilesWithFilter(dirName2, yamlFileFilter)
if err != nil {
return false, err
}
diff, err := compare.ManifestDiffWithRenameSelectIgnore(mf1, mf2, renameResources, selectResources,
ignoreResources, rootArgs.verbose)
if err != nil {
return false, err
}
if diff != "" {
fmt.Printf("Differences in manifests are:\n%s\n", diff)
return false, nil
}
fmt.Println("Manifests are identical")
return true, nil
}