forked from fluid-cloudnative/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: fix cron DataMigrate for Kubernetes 1.20 (fluid-cloudnative#3285
) * Refactor cron DataMigrate for backward compatibility Signed-off-by: trafalgarzzz <trafalgarz@outlook.com> * Install cronjob resource according to kube version Signed-off-by: trafalgarzzz <trafalgarz@outlook.com> * Fix cronjob indentation error Signed-off-by: trafalgarzzz <trafalgarz@outlook.com> * Move discovery of batch api group out of init func Signed-off-by: trafalgarzzz <trafalgarz@outlook.com> * Move discovery of batch api group out of init func Signed-off-by: trafalgarzzz <trafalgarz@outlook.com> * Add unit test for func GetCronJobStatus Signed-off-by: trafalgarzzz <trafalgarz@outlook.com> * Skip discover batch api group when running unit tests Signed-off-by: trafalgarzzz <trafalgarz@outlook.com> * Add apache licenses Signed-off-by: trafalgarzzz <trafalgarz@outlook.com> * Use helm function .Capabilities.APIVersions.Has to determine API group compatibility Signed-off-by: trafalgarzzz <trafalgarz@outlook.com> --------- Signed-off-by: trafalgarzzz <trafalgarz@outlook.com>
- Loading branch information
1 parent
307f8e2
commit 62c8293
Showing
9 changed files
with
209 additions
and
23 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
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
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
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
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
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,50 @@ | ||
/* | ||
Copyright 2023 The Fluid 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 kubeclient | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/fluid-cloudnative/fluid/pkg/utils/compatibility" | ||
batchv1 "k8s.io/api/batch/v1" | ||
batchv1beta1 "k8s.io/api/batch/v1beta1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// GetCronJobStatus gets CronJob's status given its namespace and name. It converts batchv1beta1.CronJobStatus | ||
// to batchv1.CronJobStatus when batchv1.CronJob is not supported by the cluster. | ||
func GetCronJobStatus(client client.Client, key types.NamespacedName) (*batchv1.CronJobStatus, error) { | ||
if compatibility.IsBatchV1CronJobSupported() { | ||
var cronjob batchv1.CronJob | ||
if err := client.Get(context.TODO(), key, &cronjob); err != nil { | ||
return nil, err | ||
} | ||
return &cronjob.Status, nil | ||
} | ||
|
||
var cronjob batchv1beta1.CronJob | ||
if err := client.Get(context.TODO(), key, &cronjob); err != nil { | ||
return nil, err | ||
} | ||
// Convert batchv1beta1.CronJobStatus to batchv1.CronJobStatus and return | ||
return &batchv1.CronJobStatus{ | ||
Active: cronjob.Status.Active, | ||
LastScheduleTime: cronjob.Status.LastScheduleTime, | ||
LastSuccessfulTime: cronjob.Status.LastSuccessfulTime, | ||
}, nil | ||
} |
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,110 @@ | ||
/* | ||
Copyright 2023 The Fluid 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 kubeclient | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/agiledragon/gomonkey/v2" | ||
"github.com/fluid-cloudnative/fluid/pkg/utils/compatibility" | ||
"github.com/fluid-cloudnative/fluid/pkg/utils/fake" | ||
batchv1 "k8s.io/api/batch/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
func TestGetCronJobStatus(t *testing.T) { | ||
nowTime := time.Now() | ||
testDate := metav1.NewTime(time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), nowTime.Hour(), 0, 0, 0, nowTime.Location())) | ||
|
||
namespace := "default" | ||
testCronJobInputs := []*batchv1.CronJob{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test1", | ||
Namespace: namespace, | ||
}, | ||
Status: batchv1.CronJobStatus{ | ||
LastScheduleTime: &testDate, | ||
}, | ||
}, | ||
} | ||
|
||
testCronJobs := []runtime.Object{} | ||
|
||
for _, cj := range testCronJobInputs { | ||
testCronJobs = append(testCronJobs, cj.DeepCopy()) | ||
} | ||
|
||
client := fake.NewFakeClientWithScheme(testScheme, testCronJobs...) | ||
|
||
type args struct { | ||
key types.NamespacedName | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *batchv1.CronJobStatus | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "CronJob exists", | ||
args: args{ | ||
key: types.NamespacedName{ | ||
Namespace: namespace, | ||
Name: "test1", | ||
}, | ||
}, | ||
want: &batchv1.CronJobStatus{ | ||
LastScheduleTime: &testDate, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "CronJob exists", | ||
args: args{ | ||
key: types.NamespacedName{ | ||
Namespace: namespace, | ||
Name: "test-notexist", | ||
}, | ||
}, | ||
want: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
patch := gomonkey.ApplyFunc(compatibility.IsBatchV1CronJobSupported, func() bool { | ||
return true | ||
}) | ||
defer patch.Reset() | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := GetCronJobStatus(client, tt.args.key) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetCronJobStatus() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetCronJobStatus() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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
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,26 @@ | ||
/* | ||
Copyright 2023 The Fluid 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 testutil | ||
|
||
import "os" | ||
|
||
const FluidUnitTestEnv = "FLUID_UNIT_TEST" | ||
|
||
func IsUnitTest() bool { | ||
_, exists := os.LookupEnv(FluidUnitTestEnv) | ||
return exists | ||
} |