Skip to content

Commit 0530b4a

Browse files
committed
test exec command
1 parent 6afd668 commit 0530b4a

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

pkg/apis/conventions/v1alpha1/clusterpodconvention_defaults.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ func (s *ClusterPodConventionSpec) Default() {
3434
if s.Priority == "" {
3535
s.Priority = NormalPriority
3636
}
37-
if s.Webhook != nil {
37+
38+
// only set this default if ytt is not the current configuration
39+
if s.Webhook != nil && s.Ytt == nil {
3840
s.Webhook.Default()
3941
}
4042

41-
// set default for the ytt template
42-
// if s.Ytt != "" {
43-
// s.Ytt.template = ""
44-
// }
4543
if s.SelectorTarget == "" {
4644
s.SelectorTarget = PodTemplateSpecLabels
4745
}

pkg/apis/conventions/v1alpha1/clusterpodconvention_validation.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,15 @@ func (s *ClusterPodConventionSpec) Validate() validation.FieldErrors {
7373
}
7474

7575
// The Webhook and Ytt configurations are nutually exclusive
76+
// return error if neither is provided
7677
if s.Webhook == nil && s.Ytt == nil {
7778
errs = errs.Also(validation.ErrMissingField("webhook")).Also(validation.ErrMissingField("ytt"))
7879

7980
} else {
80-
// only invoke webhook validations if the ytt configuration is not being used
81+
// only invoke webhook specific validations if the ytt configuration is not being used
8182
if s.Ytt == nil && s.Webhook != nil {
8283
errs = errs.Also(s.Webhook.Validate().ViaField("webhook"))
8384
}
84-
// only invoke ytt validations if the webhook configuration is not being used
85-
// if s.Webhook == nil && s.Ytt != nil {
86-
// errs = errs.Also(s.Ytt.Validate().ViaField("ytt"))
87-
// }
8885
}
8986

9087
if s.SelectorTarget != PodTemplateSpecLabels && s.SelectorTarget != PodIntentLabels {
@@ -96,7 +93,7 @@ func (s *ClusterPodConventionSpec) Validate() validation.FieldErrors {
9693
return errs
9794
}
9895

99-
func (s *ClusterPodConventionYttTemplate) Validate() validation.FieldErrors {
96+
func (s *ClusterPodConventionYttTemplate) ValidateYtt() validation.FieldErrors {
10097
errs := validation.FieldErrors{}
10198
if s.Template == "" {
10299
errs = errs.Also(validation.ErrMissingField("template"))

pkg/controllers/podintent_reconciler.go

+41-8
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,14 @@ func ResolveConventions() reconcilers.SubReconciler {
139139
convention.ClientConfig = *clientConfig
140140
} else if source.Spec.Ytt != nil {
141141
log.Info("handling a ytt based convention")
142-
143-
// read template spec and convert to string
144-
145142
log.Info("retrieved pod template spec from the workload", parent)
146-
stampedObj, err := ApplyYtt(ctx, *parent)
147-
if err != nil {
148-
return nil
149-
}
150-
log.Info("stamped out object", stampedObj)
143+
144+
// stampedObj, err := ApplyYtt(ctx, *parent)
145+
// if err != nil {
146+
// return nil
147+
// }
148+
// log.Info("stamped out object", stampedObj)
149+
ExecCall()
151150
}
152151
conventions = append(conventions, convention)
153152
}
@@ -165,23 +164,56 @@ func ResolveConventions() reconcilers.SubReconciler {
165164
}
166165
}
167166

167+
func ExecCall() {
168+
app := "echo"
169+
170+
arg0 := "-e"
171+
arg1 := "Hello world"
172+
arg2 := "\n\tfrom"
173+
arg3 := "golang"
174+
175+
cmd := exec.Command(app, arg0, arg1, arg2, arg3)
176+
stdout, err := cmd.Output()
177+
178+
if err != nil {
179+
fmt.Println(err.Error())
180+
return
181+
}
182+
fmt.Println(string(stdout))
183+
184+
}
185+
168186
func ApplyYtt(ctx context.Context, workload conventionsv1alpha1.PodIntent) (interface{}, error) {
187+
// read template spec from the workload
169188
template := workload.Spec.Template.AsPodTemplateSpec()
170189
log := logr.FromContextOrDiscard(ctx)
171190

191+
// set timeout to about 4 secs to process the ytt template
172192
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
173193
defer cancel()
174194

195+
// setup ytt path
175196
ytt := "ytt"
176197
if kodata, ok := os.LookupEnv("KO_DATA_PATH"); ok {
177198
ytt = path.Join(kodata, fmt.Sprintf("ytt-%s-%s", runtime.GOOS, runtime.GOARCH))
178199
}
179200

201+
// toolsBinDir := filepath.Join(projectRootDir, constants.ToolsBinDirPath)
202+
// ytt_command := exec.Command(
203+
// filepath.Join(toolsBinDir, "ytt"),
204+
// "-f-",
205+
// "-f", packageHelpersLibFile,
206+
// "-f", packageValuesFile,
207+
// "-v", "packageRepository="+packageRepository,
208+
// "-v", "registry="+registry,
209+
// ) //
210+
180211
args := []string{"--version"}
181212
stdin := bytes.NewReader([]byte(template.Spec.String()))
182213
stdout := bytes.NewBuffer([]byte{})
183214
stderr := bytes.NewBuffer([]byte{})
184215

216+
// setup exec call
185217
cmd := exec.CommandContext(ctx, ytt, args...)
186218
cmd.Stdin = stdin
187219
cmd.Stdout = stdout
@@ -190,6 +222,7 @@ func ApplyYtt(ctx context.Context, workload conventionsv1alpha1.PodIntent) (inte
190222
log.Info("ytt call args", args)
191223
log.Info("ytt call input", template)
192224

225+
// invoke run call
193226
if err := cmd.Run(); err != nil {
194227
msg := stderr.String()
195228
if msg == "" {

0 commit comments

Comments
 (0)