Skip to content

Commit d87319a

Browse files
perdasilvaPer Goncalves da Silva
and
Per Goncalves da Silva
authored
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]>
1 parent 943a726 commit d87319a

File tree

3 files changed

+355
-13
lines changed

3 files changed

+355
-13
lines changed

pkg/controller/install/certresources.go

+10-5
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

pkg/controller/operators/olm/operator.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -1888,10 +1888,19 @@ func (a *Operator) transitionCSVState(in v1alpha1.ClusterServiceVersion) (out *v
18881888
return
18891889
}
18901890

1891-
if out.HasCAResources() {
1891+
// Only update certificate status if:
1892+
// - the CSV has CAResources; and
1893+
// - the certificate lastUpdated and rotateAt timestamps have not already been set, or the certificate should be rotated
1894+
// Note: the code here is a bit wonky and it wasn't clear how to clean it up without some major refactoring:
1895+
// the installer is in charge of generating and rotating the certificates. It detects whether a certificate should be rotated by
1896+
// looking at the csv.status.RotateAt value. But, it does not update this value or surface
1897+
// the certificate expiry information. So, the rotatedAt value is to be re-calculated here. This is bad because you have
1898+
// two different components doing the same thing (installer and operator are both calculating RotateAt). If we're not careful
1899+
// there could be skew
1900+
// See pkg/controller/install/certresources.go
1901+
if shouldUpdateCertificateDates(out) {
18921902
now := metav1.Now()
1893-
expiration := now.Add(install.DefaultCertValidFor)
1894-
rotateAt := expiration.Add(-1 * install.DefaultCertMinFresh)
1903+
_, rotateAt := install.CalculateCertExpirationAndRotateAt()
18951904
rotateTime := metav1.NewTime(rotateAt)
18961905
out.Status.CertsLastUpdated = &now
18971906
out.Status.CertsRotateAt = &rotateTime
@@ -2522,3 +2531,11 @@ func (a *Operator) ensureLabels(in *v1alpha1.ClusterServiceVersion, labelSets ..
25222531
out, err := a.client.OperatorsV1alpha1().ClusterServiceVersions(out.GetNamespace()).Update(context.TODO(), out, metav1.UpdateOptions{})
25232532
return out, err
25242533
}
2534+
2535+
// shouldUpdateCertificateDates checks the csv status to decide whether
2536+
// status.CertsLastUpdated and status.CertsRotateAt should be updated
2537+
// returns true if the CSV has CAResources and status.RotatedAt is not set OR its time to rotate the certificates
2538+
func shouldUpdateCertificateDates(csv *v1alpha1.ClusterServiceVersion) bool {
2539+
isNotSet := csv.Status.CertsRotateAt == nil || csv.Status.CertsRotateAt.IsZero()
2540+
return csv.HasCAResources() && (isNotSet || install.ShouldRotateCerts(csv))
2541+
}

0 commit comments

Comments
 (0)