|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package controlapi |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/agent-substrate/substrate/internal/proto/ateletpb" |
| 19 | + atev1alpha1 "github.com/agent-substrate/substrate/pkg/api/v1alpha1" |
| 20 | +) |
| 21 | + |
| 22 | +func buildAteletWorkloadSpec(actorTemplate *atev1alpha1.ActorTemplate) *ateletpb.WorkloadSpec { |
| 23 | + workloadSpec := &ateletpb.WorkloadSpec{ |
| 24 | + PauseImage: actorTemplate.Spec.PauseImage, |
| 25 | + EgressPolicy: buildAteletEgressPolicy(actorTemplate.Spec.EgressPolicy), |
| 26 | + } |
| 27 | + for _, ctr := range actorTemplate.Spec.Containers { |
| 28 | + ateletCtr := &ateletpb.Container{ |
| 29 | + Name: ctr.Name, |
| 30 | + Image: ctr.Image, |
| 31 | + Command: ctr.Command, |
| 32 | + } |
| 33 | + for _, env := range ctr.Env { |
| 34 | + ateletCtr.Env = append(ateletCtr.Env, &ateletpb.EnvEntry{ |
| 35 | + Name: env.Name, |
| 36 | + Value: env.Value, |
| 37 | + }) |
| 38 | + } |
| 39 | + workloadSpec.Containers = append(workloadSpec.Containers, ateletCtr) |
| 40 | + } |
| 41 | + return workloadSpec |
| 42 | +} |
| 43 | + |
| 44 | +func buildAteletEgressPolicy(policy *atev1alpha1.EgressPolicy) *ateletpb.EgressPolicy { |
| 45 | + if policy == nil { |
| 46 | + return nil |
| 47 | + } |
| 48 | + return &ateletpb.EgressPolicy{ |
| 49 | + DefaultAction: string(policy.DefaultAction), |
| 50 | + Allow: buildAteletEgressPolicyRules(policy.Allow), |
| 51 | + Deny: buildAteletEgressPolicyRules(policy.Deny), |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func buildAteletEgressPolicyRules(rules []atev1alpha1.EgressPolicyRule) []*ateletpb.EgressPolicyRule { |
| 56 | + out := make([]*ateletpb.EgressPolicyRule, 0, len(rules)) |
| 57 | + for _, rule := range rules { |
| 58 | + outRule := &ateletpb.EgressPolicyRule{} |
| 59 | + for _, dest := range rule.To { |
| 60 | + outDest := &ateletpb.EgressPolicyDestination{Host: dest.Host} |
| 61 | + if dest.IPBlock != nil { |
| 62 | + outDest.Cidr = dest.IPBlock.CIDR |
| 63 | + } |
| 64 | + outRule.To = append(outRule.To, outDest) |
| 65 | + } |
| 66 | + for _, port := range rule.Ports { |
| 67 | + outRule.Ports = append(outRule.Ports, &ateletpb.EgressPort{ |
| 68 | + Port: uint32(port.Port), |
| 69 | + Protocol: string(port.Protocol), |
| 70 | + }) |
| 71 | + } |
| 72 | + out = append(out, outRule) |
| 73 | + } |
| 74 | + return out |
| 75 | +} |
0 commit comments