Skip to content

Commit

Permalink
Add support for argo rollouts
Browse files Browse the repository at this point in the history
  • Loading branch information
ContrastSecu authored and gamingrobot committed Oct 17, 2023
1 parent 5e2e6a1 commit 165b436
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/Contrast.K8s.AgentOperator/Controllers/RolloutController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Contrast Security, Inc licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using Contrast.K8s.AgentOperator.Core.Kube;
using Contrast.K8s.AgentOperator.Core.State;
using Contrast.K8s.AgentOperator.Entities;
using JetBrains.Annotations;
using k8s.Models;
using KubeOps.Operator.Controller;
using KubeOps.Operator.Rbac;

namespace Contrast.K8s.AgentOperator.Controllers
{
//[EntityRbac(typeof(V1alpha1Rollout), Verbs = VerbConstants.ReadAndPatch), UsedImplicitly]
public class RolloutController : GenericController<V1alpha1Rollout> //IResourceController<V1alpha1Rollout>
{
public RolloutController(IEventStream eventStream) : base(eventStream)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Contrast.K8s.AgentOperator.Core.State.Resources;
using Contrast.K8s.AgentOperator.Core.State.Resources.Interfaces;
using Contrast.K8s.AgentOperator.Entities.OpenShift;
using Contrast.K8s.AgentOperator.Entities;
using JetBrains.Annotations;
using k8s.Models;
using MediatR;
Expand Down Expand Up @@ -84,6 +85,7 @@ private ValueTask PatchToDesiredState(DesiredState desiredState, ResourceIdentit
{
DaemonSetResource => PatchToDesiredStateDaemonSet(desiredState, identity),
StatefulSetResource => PatchToDesiredStateStatefulSet(desiredState, identity),
RolloutResource => PatchToDesiredStateRollout(desiredState, identity),
DeploymentResource => PatchToDesiredStateDeployment(desiredState, identity),
DeploymentConfigResource => PatchToDesiredStateDeploymentConfig(desiredState, identity),
_ => throw new ArgumentOutOfRangeException()
Expand All @@ -107,7 +109,11 @@ private async ValueTask PatchToDesiredStateDeployment(DesiredState desiredState,
await _state.MarkAsDirty(identity);
await _patcher.Patch<V1Deployment>(identity.Name, identity.Namespace, o => { PatchAnnotations(desiredState, o.Spec.Template); });
}

private async ValueTask PatchToDesiredStateRollout(DesiredState desiredState, NamespacedResourceIdentity identity)
{
await _state.MarkAsDirty(identity);
await _patcher.Patch<V1alpha1Rollout>(identity.Name, identity.Namespace, o => { PatchAnnotations(desiredState, o.Spec.Template); });
}
private async ValueTask PatchToDesiredStateDeploymentConfig(DesiredState desiredState, NamespacedResourceIdentity identity)
{
await _state.MarkAsDirty(identity);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Contrast Security, Inc licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System.Threading;
using System.Threading.Tasks;
using Contrast.K8s.AgentOperator.Core.Kube;
using Contrast.K8s.AgentOperator.Core.State.Resources;
using Contrast.K8s.AgentOperator.Entities;
using JetBrains.Annotations;
using k8s.Models;
using MediatR;

namespace Contrast.K8s.AgentOperator.Core.State.Appliers
{
[UsedImplicitly]
public class RolloutApplier : BaseApplier<V1alpha1Rollout, RolloutResource>
{
public RolloutApplier(IStateContainer stateContainer, IMediator mediator) : base(stateContainer, mediator)
{
}

public override ValueTask<RolloutResource> CreateFrom(V1alpha1Rollout entity, CancellationToken cancellationToken = default)
{
var resource = new RolloutResource(
entity.Uid(),
entity. Metadata.GetLabels(),
entity.Spec.Template.GetPod(),
entity.Spec.Selector.ToPodSelector()
);

return ValueTask.FromResult(resource);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Contrast Security, Inc licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using Contrast.K8s.AgentOperator.Core.State.Resources.Interfaces;
using Contrast.K8s.AgentOperator.Core.State.Resources.Primitives;


namespace Contrast.K8s.AgentOperator.Core.State.Resources
{

public record RolloutResource(string Uid,
IReadOnlyCollection<MetadataLabel> Labels,
PodTemplate PodTemplate,
PodSelector Selector)
: IResourceWithPodTemplate;

}


30 changes: 30 additions & 0 deletions src/Contrast.K8s.AgentOperator/Entities/V1Alpha1ArgoRollout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Contrast Security, Inc licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using k8s.Models;
using k8s;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using KubeOps.Operator.Entities.Annotations;
using KubeOps.Operator.Entities;
using Contrast.K8s.AgentOperator.Core.Kube;
using JetBrains.Annotations;
using KubeOps.Operator.Rbac;

namespace Contrast.K8s.AgentOperator.Entities
{



[IgnoreEntity]
[EntityRbac(typeof(V1alpha1Rollout), Verbs = VerbConstants.ReadAndPatch), UsedImplicitly]
[KubernetesEntity(Group = "argoproj.io", ApiVersion = "v1alpha1", Kind = "Rollout", PluralName = "rollouts")]
public class V1alpha1Rollout : CustomKubernetesEntity<V1alpha1Rollout.V1alpha1rolloutSpec>
{
public class V1alpha1rolloutSpec : V1DeploymentSpec
{
//public string Host { get; set; }
}
}

}

0 comments on commit 165b436

Please sign in to comment.