Skip to content

enable formatting for the description field #353

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/controller/prefix_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}

// check if the created prefix contains the entire description from spec
// TODO: implement a different way to check if the description was truncated
if _, found := strings.CutPrefix(netboxPrefixModel.Description, req.NamespacedName.String()+" // "+prefix.Spec.Description); !found {
r.EventStatusRecorder.Recorder().Event(prefix, corev1.EventTypeWarning, "PrefixDescriptionTruncated", "prefix was created with truncated description")
}
Expand Down Expand Up @@ -291,12 +292,13 @@ func generateNetboxPrefixModelFromPrefixSpec(spec *netboxv1.PrefixSpec, req ctrl
}
}

template := config.GetOperatorConfig().DescriptionFormat
return &models.Prefix{
Prefix: spec.Prefix,
Metadata: &models.NetboxMetadata{
Comments: spec.Comments,
Custom: netboxCustomFields,
Description: req.NamespacedName.String() + " // " + spec.Description,
Description: api.FormatDescription(template, req.NamespacedName.String(), spec.Description),
Site: spec.Site,
Tenant: spec.Tenant,
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type OperatorConfig struct {
HttpsEnable bool `mapstructure:"HTTPS_ENABLE"`
DebugEnable bool `mapstructure:"DEBUG_ENABLE"`
NetboxRestorationHashFieldName string `mapstructure:"NETBOX_RESTORATION_HASH_FIELD_NAME"`
DescriptionFormat string `mapstructure:"DESCRIPTION_FORMAT"`
}

func (c *OperatorConfig) setDefaults() {
Expand All @@ -47,6 +48,7 @@ func (c *OperatorConfig) setDefaults() {
c.viper.SetDefault("HTTPS_ENABLE", true)
c.viper.SetDefault("DEBUG_ENABLE", false)
c.viper.SetDefault("NETBOX_RESTORATION_HASH_FIELD_NAME", "netboxOperatorRestorationHash")
c.viper.SetDefault("DESCRIPTION_FORMAT", "${name} ${description} ${warning}")
}

func (c *OperatorConfig) LoadCaCert() (cert []byte, err error) {
Expand Down
21 changes: 21 additions & 0 deletions pkg/netbox/api/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,24 @@ func SetIpAddressMask(ip string, ipFamily int64) (string, error) {
return "", errors.New("unknown IP family")
}
}

// FormatDescription formats a description string using the configured format template.
// It supports substitution of the following placeholders:
// * ${name}: the name of the object (e.g., "test-namespace/test-resource-name")
// * ${description}: the description provided by the user in spec.description
// * ${warning}: the static warning comment provided by the operator
//
// The default format is "${name} ${description} ${warning}".
func FormatDescription(template, name, description string) string {

// Replace placeholders
result := strings.ReplaceAll(template, "${name}", name)
result = strings.ReplaceAll(result, "${description}", description)
result = strings.ReplaceAll(result, "${warning}", warningComment)

// Truncate to the max length that Netbox allows
if utf8.RuneCountInString(result) > maxAllowedDescriptionLength {
result = result[:maxAllowedDescriptionLength]
}
return result
}
3 changes: 1 addition & 2 deletions pkg/netbox/api/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ func (r *NetboxClient) ReserveOrUpdatePrefix(prefix *models.Prefix) (*netboxMode
Prefix: &prefix.Prefix,
Comments: prefix.Metadata.Comments + warningComment,
CustomFields: prefix.Metadata.Custom,
Description: prefix.Metadata.Description + warningComment,
Description: prefix.Metadata.Description,
Status: "active",
}

if prefix.Metadata != nil {
desiredPrefix.CustomFields = prefix.Metadata.Custom
desiredPrefix.Comments = prefix.Metadata.Comments + warningComment
desiredPrefix.Description = TruncateDescription(prefix.Metadata.Description)
}

if prefix.Metadata != nil && prefix.Metadata.Tenant != "" {
Expand Down