|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | +package v1 |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/microsoftgraph/msgraph-sdk-go/models" |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + "sigs.k8s.io/controller-runtime/pkg/conversion" |
| 9 | + |
| 10 | + "github.com/Azure/azure-service-operator/v2/internal/util/to" |
| 11 | + "github.com/Azure/azure-service-operator/v2/pkg/genruntime" |
| 12 | + "github.com/Azure/azure-service-operator/v2/pkg/genruntime/conditions" |
| 13 | +) |
| 14 | + |
| 15 | +// +kubebuilder:rbac:groups=entra.azure.com,resources=securitygroups,verbs=get;list;watch;create;update;patch;delete |
| 16 | +// +kubebuilder:rbac:groups=entra.azure.com,resources={securitygroups/status,users/finalizers},verbs=get;update;patch |
| 17 | + |
| 18 | +// +kubebuilder:object:root=true |
| 19 | +// +kubebuilder:subresource:status |
| 20 | +// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status" |
| 21 | +// +kubebuilder:printcolumn:name="Severity",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].severity" |
| 22 | +// +kubebuilder:printcolumn:name="Reason",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].reason" |
| 23 | +// +kubebuilder:printcolumn:name="Message",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].message" |
| 24 | +// +kubebuilder:storageversion |
| 25 | +// SecurityGroup is an Entra Security Group. |
| 26 | +type SecurityGroup struct { |
| 27 | + metav1.TypeMeta `json:",inline"` |
| 28 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 29 | + Spec SecurityGroupSpec `json:"spec,omitempty"` |
| 30 | + Status SecurityGroupStatus `json:"status,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +var _ conditions.Conditioner = &SecurityGroup{} |
| 34 | + |
| 35 | +// GetConditions returns the conditions of the resource |
| 36 | +func (group *SecurityGroup) GetConditions() conditions.Conditions { |
| 37 | + return group.Status.Conditions |
| 38 | +} |
| 39 | + |
| 40 | +// SetConditions sets the conditions on the resource status |
| 41 | +func (group *SecurityGroup) SetConditions(conditions conditions.Conditions) { |
| 42 | + group.Status.Conditions = conditions |
| 43 | +} |
| 44 | + |
| 45 | +var _ conversion.Hub = &SecurityGroup{} |
| 46 | + |
| 47 | +// Hub marks that this userSpec is the hub type for conversion |
| 48 | +func (user *SecurityGroup) Hub() {} |
| 49 | + |
| 50 | +// +kubebuilder:object:root=true |
| 51 | +type SecurityGroupList struct { |
| 52 | + metav1.TypeMeta `json:",inline"` |
| 53 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 54 | + Items []SecurityGroup `json:"items"` |
| 55 | +} |
| 56 | + |
| 57 | +type SecurityGroupSpec struct { |
| 58 | + // DisplayName: The display name of the group. |
| 59 | + // +kubebuilder:validation:Required |
| 60 | + DisplayName *string `json:"displayName,omitempty"` |
| 61 | + |
| 62 | + // MailNickname: The email address of the group, specified either as a mail nickname (`mygroup`) |
| 63 | + // or as a full email address (`[email protected]`). |
| 64 | + // +kubebuilder:validation:Required |
| 65 | + MailNickname *string `json:"mailNickname,omitempty"` |
| 66 | + |
| 67 | + // Description: The description of the group. |
| 68 | + Description *string `json:"description,omitempty"` |
| 69 | + |
| 70 | + // MembershipType: The membership type of the group. |
| 71 | + MembershipType *SecurityGroupMembershipType `json:"membershipType,omitempty"` |
| 72 | + |
| 73 | + // OperatorSpec: The operator specific configuration for the resource. |
| 74 | + OperatorSpec *SecurityGroupOperatorSpec `json:"operatorSpec,omitempty"` |
| 75 | + |
| 76 | + // IsAssignableToRole: Indicates whether the group can be assigned to a role. |
| 77 | + IsAssignableToRole *bool `json:"isAssignableToRole,omitempty"` |
| 78 | +} |
| 79 | + |
| 80 | +// OriginalVersion returns the original API version used to create the resource. |
| 81 | +func (spec *SecurityGroupSpec) OriginalVersion() string { |
| 82 | + return GroupVersion.Version |
| 83 | +} |
| 84 | + |
| 85 | +// AssignToGroup configures the provided instance with the details of the group |
| 86 | +func (spec *SecurityGroupSpec) AssignToGroup(model models.Groupable) { |
| 87 | + model.SetSecurityEnabled(to.Ptr(true)) |
| 88 | + model.SetDisplayName(spec.DisplayName) |
| 89 | + |
| 90 | + if spec.MailNickname != nil { |
| 91 | + model.SetMailNickname(spec.MailNickname) |
| 92 | + } |
| 93 | + |
| 94 | + if spec.Description != nil { |
| 95 | + model.SetDescription(spec.Description) |
| 96 | + } |
| 97 | + |
| 98 | + // Set the membership type |
| 99 | + membershipType := SecurityGroupMembershipTypeAssigned |
| 100 | + if spec.MembershipType != nil { |
| 101 | + membershipType = *spec.MembershipType |
| 102 | + } |
| 103 | + |
| 104 | + var groupTypes []string |
| 105 | + switch membershipType { |
| 106 | + case SecurityGroupMembershipTypeAssigned: |
| 107 | + // Empty list means assigned membership |
| 108 | + case SecurityGroupMembershipTypeAssignedM365: |
| 109 | + groupTypes = []string{"Unified"} |
| 110 | + case SecurityGroupMembershipTypeDynamic: |
| 111 | + groupTypes = []string{"DynamicMembership"} |
| 112 | + case SecurityGroupMembershipTypeDynamicM365: |
| 113 | + groupTypes = []string{"Unified", "DynamicMembership"} |
| 114 | + } |
| 115 | + model.SetGroupTypes(groupTypes) |
| 116 | + |
| 117 | + // Set isAssignableToRole |
| 118 | + if spec.IsAssignableToRole != nil { |
| 119 | + model.SetIsAssignableToRole(spec.IsAssignableToRole) |
| 120 | + } |
| 121 | + |
| 122 | + // This is a security group, not a mail distribution group |
| 123 | + model.SetMailEnabled(to.Ptr(false)) |
| 124 | +} |
| 125 | + |
| 126 | +type SecurityGroupStatus struct { |
| 127 | + // EntraID: The GUID identifing the resource in Entra |
| 128 | + EntraID *string `json:"entraID,omitempty"` |
| 129 | + |
| 130 | + // DisplayName: The display name of the group. |
| 131 | + DisplayName *string `json:"displayName,omitempty"` |
| 132 | + |
| 133 | + // Conditions: The observed state of the resource |
| 134 | + Conditions []conditions.Condition `json:"conditions,omitempty"` |
| 135 | + |
| 136 | + // +kubebuilder:validation:Required |
| 137 | + // MailNickname: The email address of the group. |
| 138 | + MailNickname *string `json:"groupEmailAddress,omitempty"` |
| 139 | + |
| 140 | + // Description: The description of the group. |
| 141 | + Description *string `json:"description,omitempty"` |
| 142 | +} |
| 143 | + |
| 144 | +func (status *SecurityGroupStatus) AssignFromGroup(model models.Groupable) { |
| 145 | + if id := model.GetId(); id != nil { |
| 146 | + status.EntraID = id |
| 147 | + } |
| 148 | + |
| 149 | + if name := model.GetDisplayName(); name != nil { |
| 150 | + status.DisplayName = name |
| 151 | + } |
| 152 | + |
| 153 | + if mailNickname := model.GetMailNickname(); mailNickname != nil { |
| 154 | + status.MailNickname = mailNickname |
| 155 | + } |
| 156 | + |
| 157 | + if description := model.GetDescription(); description != nil { |
| 158 | + status.Description = description |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +// +kubebuilder:validation:Enum={"assigned","enabled","assignedm365","enabledm365"} |
| 163 | +// +kubebuilder:default=AdoptOrCreate |
| 164 | +type SecurityGroupMembershipType string |
| 165 | + |
| 166 | +const ( |
| 167 | + // SecurityGroupMembershipTypeAssigned indicates that the group is a security group with assigned members. |
| 168 | + SecurityGroupMembershipTypeAssigned SecurityGroupMembershipType = "assigned" |
| 169 | + // SecurityGroupMembershipTypeDynamic indicates that the group is a security group with dynamic membership. |
| 170 | + SecurityGroupMembershipTypeDynamic SecurityGroupMembershipType = "dynamic" |
| 171 | + // SecurityGroupMembershipTypeAssigned indicates that the group is a Microsoft 365 security group with assigned members. |
| 172 | + SecurityGroupMembershipTypeAssignedM365 SecurityGroupMembershipType = "assignedm365" |
| 173 | + // SecurityGroupMembershipTypeDynamic indicates that the group is a Microsoft 365 security group with dynamic membership. |
| 174 | + SecurityGroupMembershipTypeDynamicM365 SecurityGroupMembershipType = "dynamicm365" |
| 175 | +) |
| 176 | + |
| 177 | +type SecurityGroupOperatorSpec struct { |
| 178 | + // CreationMode: Specifies how ASO will try to create the resource. |
| 179 | + // Specify "AlwaysCreate" to always create a new security group when first reconciled. |
| 180 | + // Or specify "AdoptOrCreate" to first try to adopt an existing security group with the same display name. |
| 181 | + // If multiple security groups with the same display name are found, the resource condition will show an error. |
| 182 | + // If not specified, defaults to "AdoptOrCreate". |
| 183 | + CreationMode *CreationMode `json:"creationMode,omitempty"` |
| 184 | + |
| 185 | + // ConfigMaps specifies any config maps that should be created by the operator. |
| 186 | + ConfigMaps *SecurityGroupOperatorConfigMaps `json:"configmaps,omitempty"` |
| 187 | +} |
| 188 | + |
| 189 | +// CreationAllowed checks if the creation mode allows ASO to create a new security group. |
| 190 | +func (spec *SecurityGroupOperatorSpec) CreationAllowed() bool { |
| 191 | + if spec.CreationMode == nil { |
| 192 | + // Default is AdoptOrCreate |
| 193 | + return true |
| 194 | + } |
| 195 | + |
| 196 | + return spec.CreationMode.AllowsCreation() |
| 197 | +} |
| 198 | + |
| 199 | +// AllowsAdoption checks if the creation mode allows ASO to adopt an existing security group. |
| 200 | +func (spec *SecurityGroupOperatorSpec) AdoptionAllowed() bool { |
| 201 | + if spec.CreationMode == nil { |
| 202 | + // Default is AdoptOrCreate |
| 203 | + return true |
| 204 | + } |
| 205 | + |
| 206 | + return spec.CreationMode.AllowsAdoption() |
| 207 | +} |
| 208 | + |
| 209 | +type SecurityGroupOperatorConfigMaps struct { |
| 210 | + // EntraID: The Entra ID of the group. |
| 211 | + EntraID *genruntime.ConfigMapDestination `json:"entraID,omitempty"` |
| 212 | +} |
| 213 | + |
| 214 | +func init() { |
| 215 | + SchemeBuilder.Register(&SecurityGroup{}, &SecurityGroupList{}) |
| 216 | +} |
0 commit comments