Skip to content

Commit 6dcfbd7

Browse files
author
Amit Kumar Das
authored
fix(commander): fix int64 to float64 conversion error (#150)
Signed-off-by: AmitKumarDas <[email protected]>
1 parent b231a24 commit 6dcfbd7

File tree

4 files changed

+23
-69
lines changed

4 files changed

+23
-69
lines changed

Dockerfile

+1-35
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,3 @@
1-
# --------------------------
2-
# Test d-operators binary
3-
# --------------------------
4-
FROM golang:1.13.5 as tester
5-
6-
WORKDIR /mayadata.io/d-operators/
7-
8-
# copy go modules manifests
9-
COPY go.mod go.mod
10-
COPY go.sum go.sum
11-
12-
# ensure vendoring is up-to-date by running make vendor
13-
# in your local setup
14-
#
15-
# we cache the vendored dependencies before building and
16-
# copying source so that we don't need to re-download when
17-
# source changes don't invalidate our downloaded layer
18-
RUN go mod download
19-
RUN go mod tidy
20-
RUN go mod vendor
21-
22-
# copy build manifests
23-
COPY Makefile Makefile
24-
25-
# copy source files
26-
COPY cmd/ cmd/
27-
COPY common/ common/
28-
COPY config/ config/
29-
COPY controller/ controller/
30-
COPY pkg/ pkg/
31-
COPY types/ types/
32-
33-
# test d-operators
34-
RUN make test
35-
361
# --------------------------
372
# Build d-operators binary
383
# --------------------------
@@ -52,6 +17,7 @@ COPY go.sum go.sum
5217
# source changes don't invalidate our downloaded layer
5318
RUN go mod download
5419
RUN go mod tidy
20+
RUN go mod vendor
5521

5622
# copy build manifests
5723
COPY Makefile Makefile

cmd/commander/main.go

+18-28
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,6 @@ func main() {
110110
flag.Set("logtostderr", "true")
111111
flag.Set("alsologtostderr", "true")
112112
flag.Parse()
113-
114-
// klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
115-
// klog.InitFlags(klogFlags)
116-
117-
// Sync the glog and klog flags.
118-
// flag.CommandLine.VisitAll(func(f1 *flag.Flag) {
119-
// f2 := klogFlags.Lookup(f1.Name)
120-
// if f2 != nil {
121-
// value := f1.Value.String()
122-
// f2.Value.Set(value)
123-
// }
124-
// })
125113
defer klog.Flush()
126114

127115
if *commandName == "" {
@@ -138,7 +126,7 @@ func main() {
138126
klog.V(1).Infof("Command custom resource: namespace %q", *commandNamespace)
139127
klog.V(1).Infof("Command custom resource: name %q", *commandName)
140128

141-
r, err := NewK8sRunner()
129+
r, err := NewRunner()
142130
if err != nil {
143131
// This should lead to crashloopback if this
144132
// is running from within a Kubernetes pod
@@ -153,18 +141,19 @@ func main() {
153141
os.Exit(0)
154142
}
155143

156-
// Runnable helps in executing the Kubernetes command resource.
157-
// It does so by executing the commands or scripts specified in
158-
// the resource and updating this resource post execution.
159-
type K8sRunnable struct {
144+
// Runnable helps in executing the Kubernetes command
145+
// resource. It does so by executing the commands or scripts
146+
// specified in the resource and updating this resource post
147+
// execution.
148+
type Runnable struct {
160149
Client dynamic.Interface
161150
GVR schema.GroupVersionResource
162151

163152
commandStatus *pkg.CommandStatus
164153
}
165154

166-
// NewRunner returns a new instance of K8sRunnable
167-
func NewK8sRunner() (*K8sRunnable, error) {
155+
// NewRunner returns a new instance of Runnable
156+
func NewRunner() (*Runnable, error) {
168157
var config *rest.Config
169158
var err error
170159

@@ -198,13 +187,13 @@ func NewK8sRunner() (*K8sRunnable, error) {
198187
Resource: *commandResource,
199188
}
200189

201-
return &K8sRunnable{
190+
return &Runnable{
202191
Client: client,
203192
GVR: gvr,
204193
}, nil
205194
}
206195

207-
func (a *K8sRunnable) updateWithRetries() error {
196+
func (a *Runnable) updateWithRetries() error {
208197
var statusNew interface{}
209198
err := pkg.MarshalThenUnmarshal(a.commandStatus, &statusNew)
210199
if err != nil {
@@ -216,10 +205,12 @@ func (a *K8sRunnable) updateWithRetries() error {
216205
)
217206
}
218207
klog.V(1).Infof(
219-
"Command status: %s \n%s",
220-
pkg.NewJSON(a.commandStatus).MustMarshal(),
208+
"Command %q / %q: Status %s",
209+
*commandNamespace,
210+
*commandName,
221211
pkg.NewJSON(statusNew).MustMarshal(),
222212
)
213+
223214
// Command is updated with latest labels
224215
labels := map[string]string{
225216
// this label key is set with same value as that of status.phase
@@ -300,13 +291,12 @@ func (a *K8sRunnable) updateWithRetries() error {
300291
UpdateStatus(cmd, v1.UpdateOptions{})
301292

302293
if err == nil {
294+
// This is an extra check to detect type conversion issues
295+
// if any during later stages
303296
var c pkg.Command
304297
tErr := pkg.ToTyped(cmdUpdatedStatus, &c)
305298
klog.V(1).Infof(
306-
"IsError=%t: %v: \n%s",
307-
tErr != nil,
308-
tErr,
309-
pkg.NewJSON(c).MustMarshal(),
299+
"UnstructToTyped: IsError=%t: %v", tErr != nil, tErr,
310300
)
311301
}
312302
// If update status resulted in an error it will be
@@ -331,7 +321,7 @@ func (a *K8sRunnable) updateWithRetries() error {
331321
}
332322

333323
// Run executes the command resource
334-
func (a *K8sRunnable) Run() error {
324+
func (a *Runnable) Run() error {
335325
got, err := a.Client.
336326
Resource(a.GVR).
337327
Namespace(*commandNamespace).

cmd/commander/pkg/run_command.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,8 @@ func (r *Runnable) setStatus(out map[string]CommandOutput) {
100100
totalTimeTakenSecs := time.Duration(totalTimetaken) * time.Second
101101
totalTimeTakenSecsFmt := totalTimeTakenSecs.Round(time.Millisecond).String()
102102
r.Status.ExecutionTime = ExecutionTime{
103-
// ValueInSeconds: float64(totalTimeTakenSecs.Seconds()),
104-
//ValueInSeconds: totalTimeTakenSecs.Seconds(),
105-
ReadableValue: totalTimeTakenSecsFmt,
103+
ValueInSeconds: totalTimeTakenSecs.Seconds() + 0.0001,
104+
ReadableValue: totalTimeTakenSecsFmt,
106105
}
107106
r.Status.Outputs = out
108107
}

cmd/commander/pkg/shell_command.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,8 @@ func (l RunnableShellList) Run() map[string]CommandOutput {
219219
Stderr: stderr.String(),
220220
Stdout: stdout.String(),
221221
ExecutionTime: ExecutionTime{
222-
// ValueInSeconds: float64(timeTaken.Seconds()),
223-
//ValueInSeconds: timeTaken.Seconds(),
224-
ReadableValue: timeTakenFmt,
222+
ValueInSeconds: timeTaken.Seconds() + 0.0001,
223+
ReadableValue: timeTakenFmt,
225224
},
226225
Warning: warn,
227226
}

0 commit comments

Comments
 (0)