-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a way to use PodController object to create PodRunner object (#…
…2229) * add comments to explain the issue better * implement feedback * revert unnecessary changes * export fakePodControllerProcessor * separate fake pod controller processor fromt the test * move fake controller processor to a separate file * export struct fields to be used outside * export functions to be used outside * pull out fake PodCommandExecutorProcessor into a separate file * export pod command executor processor field * create getter for pod command executor * add podCommandExecutorProcessor to podController * add fake pod controller and fake pod command executor * export fields of fake pod controller * export fields of fake pod controller * revert redundant changes * remove commented code * add a new constructor for pod controller * add a new constructor for pod controller * address feedback * Apply suggestions from code review Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com> * address feedbacl --------- Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com>
- Loading branch information
1 parent
ae250ce
commit ae09b1f
Showing
7 changed files
with
329 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2023 The Kanister 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 kube | ||
|
||
import ( | ||
"context" | ||
"io" | ||
) | ||
|
||
type FakePodCommandExecutor struct { | ||
ExecErr error | ||
inExecCommand []string | ||
|
||
ExecStdout string | ||
ExecStderr string | ||
} | ||
|
||
// Exec | ||
func (fce *FakePodCommandExecutor) Exec(_ context.Context, command []string, stdin io.Reader, stdout, stderr io.Writer) error { | ||
fce.inExecCommand = make([]string, len(command)) | ||
copy(fce.inExecCommand, command) | ||
if stdout != nil && len(fce.ExecStdout) > 0 { | ||
stdout.Write([]byte(fce.ExecStdout)) //nolint: errcheck | ||
} | ||
if stderr != nil && len(fce.ExecStderr) > 0 { | ||
stderr.Write([]byte(fce.ExecStderr)) //nolint: errcheck | ||
} | ||
|
||
return fce.ExecErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright 2023 The Kanister 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 kube | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
type FakePodController struct { | ||
Podname string | ||
PodObj *corev1.Pod | ||
|
||
StartPodCalled bool | ||
StartPodErr error | ||
|
||
WaitForPodReadyCalled bool | ||
WaitForPodReadyErr error | ||
|
||
GetCommandExecutorRet PodCommandExecutor | ||
GetCommandExecutorErr error | ||
|
||
GetFileWriterCalled bool | ||
GetFileWriterRet *FakePodFileWriter | ||
GetFileWriterErr error | ||
|
||
StopPodCalled bool | ||
StopPodErr error | ||
InStopPodStopTimeout time.Duration | ||
InStopPodGracePeriod int64 | ||
} | ||
|
||
func (fpc *FakePodController) Pod() *corev1.Pod { | ||
return nil | ||
} | ||
|
||
func (fpc *FakePodController) PodName() string { | ||
return fpc.Podname | ||
} | ||
|
||
func (fpc *FakePodController) Run(ctx context.Context, fn func(context.Context, *corev1.Pod) (map[string]interface{}, error)) (map[string]interface{}, error) { | ||
return nil, errors.New("Not implemented") | ||
} | ||
|
||
func (fpc *FakePodController) StartPod(_ context.Context) error { | ||
fpc.StartPodCalled = true | ||
return fpc.StartPodErr | ||
} | ||
|
||
func (fpc *FakePodController) WaitForPodReady(_ context.Context) error { | ||
fpc.WaitForPodReadyCalled = true | ||
return fpc.WaitForPodReadyErr | ||
} | ||
|
||
func (fpc *FakePodController) WaitForPodCompletion(_ context.Context) error { | ||
return errors.New("Not implemented") | ||
} | ||
|
||
func (fpc *FakePodController) StreamPodLogs(_ context.Context) (io.ReadCloser, error) { | ||
return nil, errors.New("Not implemented") | ||
} | ||
|
||
func (fpc *FakePodController) GetCommandExecutor() (PodCommandExecutor, error) { | ||
return fpc.GetCommandExecutorRet, fpc.GetCommandExecutorErr | ||
} | ||
|
||
func (fpc *FakePodController) GetFileWriter() (PodFileWriter, error) { | ||
fpc.GetFileWriterCalled = true | ||
return fpc.GetFileWriterRet, fpc.GetFileWriterErr | ||
} | ||
|
||
func (fpc *FakePodController) StopPod(ctx context.Context, stopTimeout time.Duration, gracePeriodSeconds int64) error { | ||
fpc.StopPodCalled = true | ||
fpc.InStopPodStopTimeout = stopTimeout | ||
fpc.InStopPodGracePeriod = gracePeriodSeconds | ||
return fpc.StopPodErr | ||
} | ||
|
||
type FakePodFileWriter struct { | ||
writeCalled bool | ||
writeErr error | ||
writeRet *FakePodFileRemover | ||
inWriteFilePath string | ||
inWriteContent io.Reader | ||
} | ||
|
||
func (fpfw *FakePodFileWriter) Write(_ context.Context, filePath string, content io.Reader) (PodFileRemover, error) { | ||
fpfw.writeCalled = true | ||
fpfw.inWriteFilePath = filePath | ||
fpfw.inWriteContent = content | ||
|
||
return PodFileRemover(fpfw.writeRet), fpfw.writeErr | ||
} | ||
|
||
type FakePodFileRemover struct { | ||
removeCalled bool | ||
removeErr error | ||
path string | ||
} | ||
|
||
func (fr *FakePodFileRemover) Remove(_ context.Context) error { | ||
fr.removeCalled = true | ||
return fr.removeErr | ||
} | ||
|
||
func (fr *FakePodFileRemover) Path() string { | ||
return fr.path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2023 The Kanister 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 kube | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// FakePodControllerProcessor implements podControllerProcessor | ||
type FakePodControllerProcessor struct { | ||
InWaitForPodReadyNamespace string | ||
InWaitForPodReadyPodName string | ||
WaitForPodReadyErr error | ||
|
||
InWaitForPodCompletionNamespace string | ||
InWaitForPodCompletionPodName string | ||
WaitForPodCompletionErr error | ||
|
||
InDeletePodNamespace string | ||
InDeletePodPodName string | ||
InDeletePodOptions metav1.DeleteOptions | ||
DeletePodErr error | ||
|
||
InCreatePodCli kubernetes.Interface | ||
InCreatePodOptions *PodOptions | ||
CreatePodRet *corev1.Pod | ||
CreatePodErr error | ||
} | ||
|
||
func (f *FakePodControllerProcessor) CreatePod(_ context.Context, cli kubernetes.Interface, options *PodOptions) (*corev1.Pod, error) { | ||
f.InCreatePodCli = cli | ||
f.InCreatePodOptions = options | ||
return f.CreatePodRet, f.CreatePodErr | ||
} | ||
|
||
func (f *FakePodControllerProcessor) WaitForPodCompletionPCP(ctx context.Context, namespace, podName string) error { | ||
f.InWaitForPodCompletionNamespace = namespace | ||
f.InWaitForPodCompletionPodName = podName | ||
return f.WaitForPodCompletionErr | ||
} | ||
|
||
func (f *FakePodControllerProcessor) WaitForPodReadyPCP(ctx context.Context, namespace, podName string) error { | ||
f.InWaitForPodReadyPodName = podName | ||
f.InWaitForPodReadyNamespace = namespace | ||
return f.WaitForPodReadyErr | ||
} | ||
|
||
func (f *FakePodControllerProcessor) DeletePod(_ context.Context, namespace string, podName string, opts metav1.DeleteOptions) error { | ||
f.InDeletePodNamespace = namespace | ||
f.InDeletePodPodName = podName | ||
f.InDeletePodOptions = opts | ||
|
||
return f.DeletePodErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.