mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-02 03:08:15 +00:00
The current dryrun client implemnetation is suboptimal and sparse. It has the following problems: - When an object CREATE or UPDATE reaches the default dryrun client the operation is a NO-OP, which means subsequent GET calls must fully emulate the object that exists in the store. - There are multiple implmentations of a DryRunGetter interface such the one in init_dryrun.go but there are no implementations for reset, upgrade, join. - There is a specific DryRunGetter that is backed by a real client in clientbacked_dryrun.go, but this is used for upgrade and does not work in conjuction with a fake client. This commit does the following changes: - Removes all existing *dryrun*.go implementations. - Add a new DryRun implementation in dryrun.go that implements 3 clients - fake clientset, real clientset, real dynamic client. - The DryRun object uses the method chaining pattern. - Allows the user opt-in into real clients only if needed, by passing a real kubeconfig. By default only constructs a fake client. - The default reactor chain for the fake client, always logs the object action, then for GET or LIST actions attempts to use the real dynamic client to get the object. If a real object does not exist it attempts to get the object from the fake object store. - The user can prepend or append reactors to the chain. - All known needed reactors for operations during init, join, reset, upgrade are added as methods of the DryRun struct. - Adds detailed unit test for the DryRun struct and its methods including reactors. Additional changes: - Use the new DryRun implementation in all command workflows - init, join, reset, upgrade. - Ensure that --dry-run works even if there is no active cluster by returning faked objects. For join, a faked cluster-info with a fake bootstrap token and CA are used.
128 lines
2.8 KiB
Go
128 lines
2.8 KiB
Go
/*
|
|
Copyright 2024 The Kubernetes 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 dryrun
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestPrintDryRunFiles(t *testing.T) {
|
|
tmpdir, err := os.MkdirTemp("", "")
|
|
if err != nil {
|
|
t.Fatalf("Couldn't create tmpdir: %v", err)
|
|
}
|
|
defer func() {
|
|
if err = os.RemoveAll(tmpdir); err != nil {
|
|
t.Fatalf("Couldn't remove tmpdir: %v", err)
|
|
}
|
|
}()
|
|
fileContents := "apiVersion: kubeadm.k8s.io/unknownVersion"
|
|
filename := "testfile"
|
|
cfgPath := filepath.Join(tmpdir, filename)
|
|
err = os.WriteFile(cfgPath, []byte(fileContents), 0644)
|
|
if err != nil {
|
|
t.Fatalf("Couldn't write context to file: %v", err)
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
files []FileToPrint
|
|
wantW string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "RealPath is empty",
|
|
files: []FileToPrint{
|
|
{
|
|
RealPath: "",
|
|
PrintPath: cfgPath,
|
|
},
|
|
},
|
|
wantW: "",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "RealPath is a file that does not exist",
|
|
files: []FileToPrint{
|
|
{
|
|
RealPath: tmpdir + "/missingfile",
|
|
PrintPath: cfgPath,
|
|
},
|
|
},
|
|
wantW: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "RealPath is a readable file",
|
|
files: []FileToPrint{
|
|
{
|
|
RealPath: cfgPath,
|
|
PrintPath: "",
|
|
},
|
|
},
|
|
wantW: "[dryrun] Would write file \"" + cfgPath + "\" with content:\n" +
|
|
"apiVersion: kubeadm.k8s.io/unknownVersion",
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
w := &bytes.Buffer{}
|
|
if err := PrintDryRunFiles(tt.files, w); (err != nil) != tt.wantErr {
|
|
t.Errorf("error: %v, expected error: %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if gotW := w.String(); gotW != tt.wantW {
|
|
t.Errorf("\noutput: %q\nexpected output: %q", gotW, tt.wantW)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewFileToPrint(t *testing.T) {
|
|
tests := []struct {
|
|
realPath string
|
|
printPath string
|
|
want FileToPrint
|
|
}{
|
|
{
|
|
realPath: "",
|
|
printPath: "",
|
|
want: FileToPrint{},
|
|
},
|
|
{
|
|
realPath: "/etc/kubernetes",
|
|
printPath: "/tmp/kubernetes",
|
|
want: FileToPrint{
|
|
"/etc/kubernetes",
|
|
"/tmp/kubernetes",
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run("TestNewFileToPrint", func(t *testing.T) {
|
|
if got := NewFileToPrint(tt.realPath, tt.printPath); !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("got: %v, want: %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|