Skip to content

Commit 16bb6ce

Browse files
authored
Merge pull request #7 from thomashorrobin/generate-job-from-cronjob
Add CronJob/generateJob() and GenerateRandomHash
2 parents 123487e + 97ab543 commit 16bb6ce

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Copyright 2020 Swiftkube Project
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
import Foundation
18+
19+
public extension batch.v1.CronJob {
20+
func generateJob(withName name: String = "manual") throws -> batch.v1.Job {
21+
guard let jobTemplateSpec = spec?.jobTemplate.spec else { throw SwiftkubeModelError.fieldDoesntExist("Job spec") }
22+
guard let cronJobName = self.name else { throw SwiftkubeModelError.fieldDoesntExist("Cronjob Name") }
23+
let jobName = "\(cronJobName)-\(name)-\(GenerateRandomHash(length: 3))"
24+
guard let metadata = metadata else { throw SwiftkubeModelError.fieldDoesntExist("Job Metadata") }
25+
var existingMetadata = metadata
26+
existingMetadata.name = jobName
27+
var job = batch.v1.Job()
28+
existingMetadata.resourceVersion = nil
29+
job.spec = jobTemplateSpec
30+
job.metadata = existingMetadata
31+
return job
32+
}
33+
}

Sources/Extensions/Hashes.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright 2020 Swiftkube Project
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
import Foundation
18+
19+
/// GenerateRandomHash returns a three character. I
20+
func GenerateRandomHash(length: Int) -> String {
21+
if length == 0 {
22+
return ""
23+
}
24+
// We omit vowels from the set of available characters to reduce the chances
25+
// of "bad words" being formed.
26+
let alphanums = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z", "2", "4", "5", "6", "7", "8", "9"]
27+
var seedAlphanum = alphanums.randomElement()!
28+
var slug = seedAlphanum
29+
var neededAlphanums = length - 1
30+
while neededAlphanums > 0 {
31+
let nextAlphanum = alphanums.randomElement()!
32+
if nextAlphanum != seedAlphanum {
33+
seedAlphanum = nextAlphanum
34+
slug = slug + nextAlphanum
35+
neededAlphanums -= 1
36+
}
37+
}
38+
return slug
39+
}

Sources/SwiftkubeModel.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ public enum SwiftkubeModelError: Error {
2222
case unknownAPIObject(String)
2323
/// Thrown on decoding errors.
2424
case decodingError(String)
25+
/// Thrown when a Kubernetes object is missing a field that is requied to perform an operation
26+
case fieldDoesntExist(String)
2527
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Copyright 2020 Swiftkube Project
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
@testable import SwiftkubeModel
18+
import XCTest
19+
20+
private func dummyCronJob() -> batch.v1.CronJob {
21+
let jobTemplate = batch.v1.JobTemplateSpec(metadata: meta.v1.ObjectMeta(), spec: batch.v1.JobSpec(template: core.v1.PodTemplateSpec()))
22+
23+
return batch.v1.CronJob(metadata: meta.v1.ObjectMeta(clusterName: "directly-apply-main-cluster", creationTimestamp: Date(), deletionGracePeriodSeconds: 100, managedFields: [meta.v1.ManagedFieldsEntry](), name: "great-cronjob", namespace: "default", ownerReferences: [meta.v1.OwnerReference](), resourceVersion: "appv1", uid: "F3493650-A9DF-410F-B1A4-E8F5386E5B53"), spec: batch.v1.CronJobSpec(failedJobsHistoryLimit: 5, jobTemplate: jobTemplate, schedule: "15 10 * * *", startingDeadlineSeconds: 100, successfulJobsHistoryLimit: 2, suspend: false), status: batch.v1.CronJobStatus(active: [core.v1.ObjectReference](), lastScheduleTime: Date()))
24+
}
25+
26+
// MARK: - CronJobExtensionsTests
27+
28+
final class CronJobExtensionsTests: XCTestCase {
29+
30+
func testGetJobFromCronjob() throws {
31+
let exampleCronJob = dummyCronJob()
32+
let job = try exampleCronJob.generateJob()
33+
XCTAssertTrue((job.name?.starts(with: "great-cronjob-manual-")) != nil)
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Copyright 2020 Swiftkube Project
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
@testable import SwiftkubeModel
18+
import XCTest
19+
20+
let exampleHash = GenerateRandomHash(length: 3)
21+
22+
let exampleZeroLengthHash = GenerateRandomHash(length: 0)
23+
24+
// MARK: - HashesTests
25+
26+
final class HashesTests: XCTestCase {
27+
28+
func testGenerateRandomHash() throws {
29+
30+
XCTAssertEqual(exampleHash.count, 3)
31+
XCTAssertEqual(exampleZeroLengthHash.count, 0)
32+
}
33+
}

0 commit comments

Comments
 (0)