deployment.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package kubemodel
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // @bingen:generate:Deployment
  7. // Deployment represents a Kubernetes Deployment resource
  8. type Deployment struct {
  9. UID string `json:"uid"`
  10. NamespaceUID string `json:"namespaceUid"`
  11. Name string `json:"name"`
  12. Labels map[string]string `json:"labels,omitempty"`
  13. Annotations map[string]string `json:"annotations,omitempty"`
  14. MatchLabels map[string]string `json:"matchLabels"`
  15. Start time.Time `json:"start"`
  16. End time.Time `json:"end"`
  17. }
  18. func (d *Deployment) ValidateDeployment(window Window) error {
  19. if d.UID == "" {
  20. return fmt.Errorf("UID is missing for Deployment with name '%s'", d.Name)
  21. }
  22. if d.Name == "" {
  23. return fmt.Errorf("Name is missing for Deployment '%s'", d.UID)
  24. }
  25. if d.NamespaceUID == "" {
  26. return fmt.Errorf("NamespaceUID is missing for Deployment '%s'", d.UID)
  27. }
  28. if err := checkWindow(window, d.Start, d.End); err != nil {
  29. return err
  30. }
  31. return nil
  32. }
  33. func (kms *KubeModelSet) RegisterDeployment(deployment *Deployment) error {
  34. if err := deployment.ValidateDeployment(kms.Window); err != nil {
  35. err = fmt.Errorf("RegisterDeployment: invalid deployment: %w", err)
  36. kms.Error(err)
  37. return err
  38. }
  39. if _, ok := kms.Deployments[deployment.UID]; !ok {
  40. if kms.Cluster == nil {
  41. kms.Warnf("RegisterDeployment: Cluster is nil")
  42. }
  43. kms.Deployments[deployment.UID] = deployment
  44. kms.Metadata.ObjectCount++
  45. }
  46. return nil
  47. }