Skip to content

Commit 4bf7fe7

Browse files
perdasilvaPer Goncalves da Silva
andcommitted
Fix csv.status.RotatedAt update (#2752)
Signed-off-by: Per Goncalves da Silva <[email protected]> Signed-off-by: perdasilva <[email protected]> Co-authored-by: Per Goncalves da Silva <[email protected]> Upstream-repository: operator-lifecycle-manager Upstream-commit: d87319abbd63a0553bd6d37f9125faba7bd40fd5
1 parent 60cd997 commit 4bf7fe7

File tree

5 files changed

+385
-21
lines changed

5 files changed

+385
-21
lines changed

staging/operator-lifecycle-manager/pkg/controller/install/certresources.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,12 @@ func (i *StrategyDeploymentInstaller) installCertRequirements(strategy Strategy)
185185
}
186186

187187
// Create the CA
188-
expiration := time.Now().Add(DefaultCertValidFor)
188+
expiration, _ := CalculateCertExpirationAndRotateAt()
189189
ca, err := certs.GenerateCA(expiration, Organization)
190190
if err != nil {
191191
logger.Debug("failed to generate CA")
192192
return nil, err
193193
}
194-
rotateAt := expiration.Add(-1 * DefaultCertMinFresh)
195194

196195
for n, sddSpec := range strategyDetailsDeployment.DeploymentSpecs {
197196
certResources := i.certResourcesForDeployment(sddSpec.Name)
@@ -202,7 +201,7 @@ func (i *StrategyDeploymentInstaller) installCertRequirements(strategy Strategy)
202201
}
203202

204203
// Update the deployment for each certResource
205-
newDepSpec, caPEM, err := i.installCertRequirementsForDeployment(sddSpec.Name, ca, rotateAt, sddSpec.Spec, getServicePorts(certResources))
204+
newDepSpec, caPEM, err := i.installCertRequirementsForDeployment(sddSpec.Name, ca, expiration, sddSpec.Spec, getServicePorts(certResources))
206205
if err != nil {
207206
return nil, err
208207
}
@@ -223,7 +222,13 @@ func ShouldRotateCerts(csv *v1alpha1.ClusterServiceVersion) bool {
223222
return false
224223
}
225224

226-
func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deploymentName string, ca *certs.KeyPair, rotateAt time.Time, depSpec appsv1.DeploymentSpec, ports []corev1.ServicePort) (*appsv1.DeploymentSpec, []byte, error) {
225+
func CalculateCertExpirationAndRotateAt() (expiration time.Time, rotateAt time.Time) {
226+
expiration = time.Now().Add(DefaultCertValidFor)
227+
rotateAt = expiration.Add(-1 * DefaultCertMinFresh)
228+
return
229+
}
230+
231+
func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deploymentName string, ca *certs.KeyPair, expiration time.Time, depSpec appsv1.DeploymentSpec, ports []corev1.ServicePort) (*appsv1.DeploymentSpec, []byte, error) {
227232
logger := log.WithFields(log.Fields{})
228233

229234
// Create a service for the deployment
@@ -263,7 +268,7 @@ func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deplo
263268
fmt.Sprintf("%s.%s", service.GetName(), i.owner.GetNamespace()),
264269
fmt.Sprintf("%s.%s.svc", service.GetName(), i.owner.GetNamespace()),
265270
}
266-
servingPair, err := certGenerator.Generate(rotateAt, Organization, ca, hosts)
271+
servingPair, err := certGenerator.Generate(expiration, Organization, ca, hosts)
267272
if err != nil {
268273
logger.Warnf("could not generate signed certs for hosts %v", hosts)
269274
return nil, nil, err

staging/operator-lifecycle-manager/pkg/controller/operators/olm/operator.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,10 +1893,19 @@ func (a *Operator) transitionCSVState(in v1alpha1.ClusterServiceVersion) (out *v
18931893
return
18941894
}
18951895

1896-
if out.HasCAResources() {
1896+
// Only update certificate status if:
1897+
// - the CSV has CAResources; and
1898+
// - the certificate lastUpdated and rotateAt timestamps have not already been set, or the certificate should be rotated
1899+
// Note: the code here is a bit wonky and it wasn't clear how to clean it up without some major refactoring:
1900+
// the installer is in charge of generating and rotating the certificates. It detects whether a certificate should be rotated by
1901+
// looking at the csv.status.RotateAt value. But, it does not update this value or surface
1902+
// the certificate expiry information. So, the rotatedAt value is to be re-calculated here. This is bad because you have
1903+
// two different components doing the same thing (installer and operator are both calculating RotateAt). If we're not careful
1904+
// there could be skew
1905+
// See pkg/controller/install/certresources.go
1906+
if shouldUpdateCertificateDates(out) {
18971907
now := metav1.Now()
1898-
expiration := now.Add(install.DefaultCertValidFor)
1899-
rotateAt := expiration.Add(-1 * install.DefaultCertMinFresh)
1908+
_, rotateAt := install.CalculateCertExpirationAndRotateAt()
19001909
rotateTime := metav1.NewTime(rotateAt)
19011910
out.Status.CertsLastUpdated = &now
19021911
out.Status.CertsRotateAt = &rotateTime
@@ -2491,3 +2500,11 @@ func (a *Operator) ensureLabels(in *v1alpha1.ClusterServiceVersion, labelSets ..
24912500
out, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(out.GetNamespace()).Update(context.TODO(), out, metav1.UpdateOptions{})
24922501
return out, err
24932502
}
2503+
2504+
// shouldUpdateCertificateDates checks the csv status to decide whether
2505+
// status.CertsLastUpdated and status.CertsRotateAt should be updated
2506+
// returns true if the CSV has CAResources and status.RotatedAt is not set OR its time to rotate the certificates
2507+
func shouldUpdateCertificateDates(csv *v1alpha1.ClusterServiceVersion) bool {
2508+
isNotSet := csv.Status.CertsRotateAt == nil || csv.Status.CertsRotateAt.IsZero()
2509+
return csv.HasCAResources() && (isNotSet || install.ShouldRotateCerts(csv))
2510+
}

0 commit comments

Comments
 (0)