-
Notifications
You must be signed in to change notification settings - Fork 20
Use script pod template custom resource to create pods and containers #1136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fa97c83
Use script pod template custom resource to create pods and containers
APErebus 51a6990
Change to add range
APErebus cd50771
Change to new merge method
APErebus 10e42d9
Tweak how we create the pod details using new crd structure
APErebus 4ad59c2
Add clone
liam-mackie da70a66
Update octopus home tp script
liam-mackie 8a024a5
Fix compile
APErebus fc71e6a
Handle all exceptions
APErebus bb9a668
Fix NRE
APErebus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
source/Octopus.Tentacle/Kubernetes/KubernetesContainerExtensionMethods.cs
This file contains hidden or 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,21 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using k8s; | ||
using k8s.Models; | ||
|
||
namespace Octopus.Tentacle.Kubernetes | ||
{ | ||
public static class KubernetesContainerExtensionMethods | ||
{ | ||
[return: NotNullIfNotNull(nameof(source))] | ||
public static V1Container? Clone(this V1Container? source) | ||
{ | ||
if (source is null) | ||
{ | ||
return null; | ||
} | ||
// Use JSON serialization for deep cloning | ||
var json = KubernetesJson.Serialize(source); | ||
return KubernetesJson.Deserialize<V1Container>(json); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
source/Octopus.Tentacle/Kubernetes/KubernetesCustomResourceService.cs
This file contains hidden or 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,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using k8s; | ||
using k8s.Models; | ||
using Octopus.Tentacle.Core.Diagnostics; | ||
|
||
namespace Octopus.Tentacle.Kubernetes | ||
{ | ||
public interface IKubernetesCustomResourceService | ||
{ | ||
Task<ScriptPodTemplateCustomResource?> GetOldestScriptPodTemplateCustomResource(CancellationToken cancellationToken); | ||
} | ||
|
||
public class KubernetesCustomResourceService : KubernetesService, IKubernetesCustomResourceService | ||
{ | ||
public KubernetesCustomResourceService(IKubernetesClientConfigProvider configProvider, ISystemLog log) | ||
: base(configProvider, log) | ||
{ | ||
} | ||
|
||
public async Task<ScriptPodTemplateCustomResource?> GetOldestScriptPodTemplateCustomResource(CancellationToken cancellationToken) | ||
{ | ||
return await RetryPolicy.ExecuteAsync(async () => | ||
{ | ||
ScriptPodTemplateCustomResourceList resourceList; | ||
try | ||
{ | ||
resourceList = await Client.CustomObjects.ListNamespacedCustomObjectAsync<ScriptPodTemplateCustomResourceList>( | ||
"agent.octopus.com", | ||
"v1beta1", | ||
KubernetesConfig.Namespace, | ||
"scriptpodtemplates", | ||
cancellationToken: cancellationToken); | ||
} | ||
catch (Exception ex) | ||
{ | ||
// we are happy to handle all exceptions here and just fallback | ||
Log.WarnFormat(ex, "Failed to retrieve 'scriptpodtemplates' custom resource"); | ||
return null; | ||
} | ||
|
||
return resourceList.Items.OrderBy(r => r.Metadata.CreationTimestamp).FirstOrDefault(); | ||
}); | ||
} | ||
|
||
//These are only ever deserialized from JSON | ||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. | ||
class ScriptPodTemplateCustomResourceList : KubernetesObject | ||
{ | ||
public V1ListMeta Metadata { get; set; } | ||
public List<ScriptPodTemplateCustomResource> Items { get; set; } | ||
} | ||
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable. | ||
} | ||
} |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.