Skip to content

Commit b17ccfd

Browse files
committed
run-resourcewatch: Add --to-json and --from-json
These allow: * writing to a json file instead of a git repository * reading from a json file instead of an api-server
1 parent dd97f1b commit b17ccfd

File tree

4 files changed

+222
-77
lines changed

4 files changed

+222
-77
lines changed

pkg/cmd/openshift-tests/run_resource_watch/resourcewatch.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package run_resource_watch
22

33
import (
4+
"fmt"
5+
46
"github.com/openshift/origin/pkg/resourcewatch/operator"
57
"github.com/spf13/cobra"
68
"k8s.io/kubectl/pkg/util/templates"
79
)
810

911
func NewRunResourceWatchCommand() *cobra.Command {
12+
var toJsonPath, fromJsonPath string
13+
1014
cmd := &cobra.Command{
1115
Use: "run-resourcewatch",
1216
Short: "Run watch for resource changes and commit each to a git repository",
@@ -22,12 +26,22 @@ func NewRunResourceWatchCommand() *cobra.Command {
2226

2327
SilenceUsage: true,
2428
SilenceErrors: true,
29+
PreRunE: func(cmd *cobra.Command, args []string) error {
30+
// Check for mutual exclusivity of --to-json and --from-json
31+
if toJsonPath != "" && fromJsonPath != "" {
32+
return fmt.Errorf("--to-json and --from-json are mutually exclusive")
33+
}
34+
return nil
35+
},
2536
RunE: func(cmd *cobra.Command, args []string) error {
26-
return operator.RunResourceWatch()
37+
return operator.RunResourceWatch(toJsonPath, fromJsonPath)
2738
},
2839
}
2940
var dummy string
3041
cmd.Flags().StringVar(&dummy, "kubeconfig", "", "This option is not used any more. It will be removed in later releases")
3142
cmd.Flags().StringVar(&dummy, "namespace", "", "This option is not used any more. It will be removed in later releases")
43+
cmd.Flags().StringVar(&toJsonPath, "to-json", "", "Path to JSON file for output (mutually exclusive with --from-json)")
44+
cmd.Flags().StringVar(&fromJsonPath, "from-json", "", "Path to JSON file for input (mutually exclusive with --to-json)")
45+
3246
return cmd
3347
}

pkg/resourcewatch/observe/observe.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,14 @@ func emitUpdate(observedResources map[types.UID]*resourceMeta, gvr schema.GroupV
136136
}
137137

138138
resourceC <- &ResourceObservation{
139-
GroupVersionResource: gvr,
140-
UID: resource.GetUID(),
141-
ObservationType: observationType,
142-
Object: resource,
143-
OldObject: observedMeta.lastObserved,
144-
ObservationTime: time.Now(),
139+
Group: gvr.Group,
140+
Version: gvr.Version,
141+
Resource: gvr.Resource,
142+
UID: resource.GetUID(),
143+
ObservationType: observationType,
144+
Object: resource,
145+
OldObject: observedMeta.lastObserved,
146+
ObservationTime: time.Now(),
145147
}
146148

147149
observedMeta.resourceVersions[resource.GetResourceVersion()] = struct{}{}
@@ -152,11 +154,13 @@ func emitDelete(observedResources map[types.UID]*resourceMeta, gvr schema.GroupV
152154
delete(observedResources, resource.GetUID())
153155

154156
resourceC <- &ResourceObservation{
155-
GroupVersionResource: gvr,
156-
UID: resource.GetUID(),
157-
ObservationType: ObservationTypeDelete,
158-
Object: resource,
159-
ObservationTime: time.Now(),
157+
Group: gvr.Group,
158+
Version: gvr.Version,
159+
Resource: gvr.Resource,
160+
UID: resource.GetUID(),
161+
ObservationType: ObservationTypeDelete,
162+
Object: resource,
163+
ObservationTime: time.Now(),
160164
}
161165
}
162166

@@ -171,11 +175,13 @@ func emitInferredDelete(observedResources map[types.UID]*resourceMeta, lastObser
171175
tombstone.SetUID(uid)
172176

173177
resourceC <- &ResourceObservation{
174-
GroupVersionResource: gvr,
175-
UID: uid,
176-
ObservationType: ObservationTypeDelete,
177-
ObservationTime: time.Now(),
178-
Object: &tombstone,
178+
Group: gvr.Group,
179+
Version: gvr.Version,
180+
Resource: gvr.Resource,
181+
UID: uid,
182+
ObservationType: ObservationTypeDelete,
183+
ObservationTime: time.Now(),
184+
Object: &tombstone,
179185
}
180186
}
181187

pkg/resourcewatch/observe/types.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"time"
55

66
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
7-
"k8s.io/apimachinery/pkg/runtime/schema"
87
"k8s.io/apimachinery/pkg/types"
98
)
109

@@ -17,15 +16,15 @@ const (
1716
)
1817

1918
type ResourceObservation struct {
20-
schema.GroupVersionResource
19+
Group string `json:"group"`
20+
Version string `json:"version"`
21+
Resource string `json:"resource"`
2122

22-
UID types.UID
23-
Namespace string
24-
Name string
23+
UID types.UID `json:"uid"`
2524

26-
Object *unstructured.Unstructured
27-
OldObject *unstructured.Unstructured
25+
Object *unstructured.Unstructured `json:"object,omitempty"`
26+
OldObject *unstructured.Unstructured `json:"oldObject,omitempty"`
2827

29-
ObservationType ObservationType
30-
ObservationTime time.Time
28+
ObservationType ObservationType `json:"observationType"`
29+
ObservationTime time.Time `json:"observationTime"`
3130
}

0 commit comments

Comments
 (0)